<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\PermissionController; // Remove permission controller soon.

class InvoiceController extends Controller {
    private $objAuth;
    private $objUser;
    private $objPermission;

    // Constructor
    public function __construct() {
        $this->objAuth = new AuthController();
        $this->objUser = new UserController();
        $this->objPermission = new PermissionController();
    }

    // Company
    public function getCompanies(Request $r) { // /api/rpc/invoice/company/getall
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_mancompany'] == 1 && $valid['inv_manuser'] == 1) {
                return DB::table('inv_company')
                    ->select(
                        'id',
                        'user_id',
                        'name',
                        'compreg',
                        'taxnr',
                        'bank_number',
                        'bank_name',
                        'bank_recipient',
                        'logo',
                        'payterm',
                        'date_format'
                    )
                    ->get();
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function getCompany($id, Request $r) { // /api/rpc/invoice/company/get/id
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_mancompany'] == 1) {
                if ($valid['inv_manuser'] == 0) {
                    if ($id == $check) {
                        return DB::table('inv_company')
                            ->select(
                                'name',
                                'compreg',
                                'taxnr',
                                'bank_number',
                                'bank_name',
                                'bank_recipient',
                                'logo',
                                'payterm',
                                'date_format',
                                'comment1',
                                'comment2'
                            )
                            ->where('id', $id)
                            ->where('user_id', $check)
                            ->get();
                    }
                    else {
                        return 'Permission denied.';
                    }
                }
                else if ($valid['inv_manuser'] == 1) {
                    return DB::table('inv_company')
                        ->select(
                            'user_id',
                            'name',
                            'compreg',
                            'taxnr',
                            'bank_number',
                            'bank_name',
                            'bank_recipient',
                            'logo',
                            'payterm',
                            'date_format',
                            'comment1',
                            'comment2'
                        )
                        ->where('id', $id)
                        ->get();
                }
                else {
                    return 'Permission denied.';
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function newCompany(Request $r) { // /api/rpc/invoice/company/new
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_mancompany'] == 1 && $valid['inv_manuser'] == 1) {
                $add = DB::table('inv_company')
                    ->insertGetId([
                        'user_id' => $r->user_id,
                        'name' => $r->name,
                        'compreg' => ($r->compreg ? $r->compreg : ''),
                        'taxnr' => ($r->taxnr ? $r->taxnr : ''),
                        'bank_number' => $r->bank_number,
                        'bank_name' => $r->bank_name,
                        'bank_recipient' => $r->bank_recipient,
                        'logo' => ($r->logo ? $r->logo : ''),
                        'payterm' => $r->payterm,
                        'date_format' => $r->date_format,
                        'comment1' => $r->comment1,
                        'comment2' => $r->comment2
                    ]);

                return $add;
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function editCompany(Request $r) { // /api/rpc/invoice/company/edit
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_mancompany'] == 1) {
                if ($valid['inv_manuser'] == 0) {
                    if ($companyId != 0) {
                        DB::table('inv_company')
                            ->where('id', $r->id)
                            ->where('user_id', $check)
                            ->update([
                                'name' => $r->name,
                                'compreg' => ($r->compreg ? $r->compreg : ''),
                                'taxnr' => ($r->taxnr ? $r->taxnr : ''),
                                'bank_number' => $r->bank_number,
                                'bank_name' => $r->bank_name,
                                'bank_recipient' => $r->bank_recipient,
                                'logo' => ($r->logo ? $r->logo : ''),
                                'payterm' => $r->payterm,
                                'date_format' => $r->date_format,
                                'comment1' => $r->comment1,
                                'comment2' => $r->comment2
                            ]);
                    }
                    else {
                        return 'Permission denied.';
                    }
                }
                else if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_company')
                        ->where('id', $r->id)
                        ->update([
                            'user_id' => $r->user_id,
                            'name' => $r->name,
                            'compreg' => ($r->compreg ? $r->compreg : ''),
                            'taxnr' => ($r->taxnr ? $r->taxnr : ''),
                            'bank_number' => $r->bank_number,
                            'bank_name' => $r->bank_name,
                            'bank_recipient' => $r->bank_recipient,
                            'logo' => ($r->logo ? $r->logo : ''),
                            'payterm' => $r->payterm,
                            'date_format' => $r->date_format,
                            'comment1' => $r->comment1,
                            'comment2' => $r->comment2
                        ]);
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function deleteCompany(Request $r) { // /api/rpc/invoice/company/delete
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_mancompany'] == 1 && $valid['inv_manuser'] == 1) {
                DB::table('inv_company')->where('id', $r->id)->delete();

                return 'Done.';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    // Contacts
    public function getContacts(Request $r) { // /api/rpc/invoice/contacts/getall
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_mancontact'] == 1) {
                $data = array(
                    'inv_contacts.id',
                    'inv_contacts.user_id',
                    'inv_contacts.company_name',
                    'inv_contacts.name',
                    'inv_contacts.fulladdress',
                    'inv_contacts.phone',
                    'inv_contacts.email'
               );
                if ($valid['inv_manuser'] == 1) {

                    if (isset($r->emp)) {
                        $get = DB::table('inv_contacts')
                            ->join('inv_employers', 'inv_employers.contact_id', 'inv_contacts.id')
                            ->get($data);
                    }
                    else if (isset($r->cus)) {
                        $get = DB::table('inv_contacts')
                            ->join('inv_clients', 'inv_clients.contact_id', 'inv_contacts.id')
                            ->get($data);
                        }
                    else {
                        $get = DB::table('inv_contacts')
                            ->select(
                                'id',
                                'user_id',
                                'company_name',
                                'name',
                                'fulladdress',
                                'phone',
                                'email'
                            )
                            ->get();
                    }

                    return $get;
                }
                else {
                    $get = DB::table('inv_contacts')
                        ->select(
                            'id',
                            'company_name',
                            'name',
                            'fulladdress',
                            'phone',
                            'email'
                        )
                        ->where('user_id', $check)
                        ->get();

                    return $get;
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function getContact($id, Request $r) { // /api/rpc/invoice/contacts/get/id
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_mancontact'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    $get = DB::table('inv_contacts')
                        ->select(
                            'id',
                            'user_id',
                            'company_name',
                            'name',
                            'fulladdress',
                            'phone',
                            'website',
                            'email',
                            'custom'
                        )
                        ->where('id', $id)
                        ->get();

                    $emp = DB::table('inv_employers')
                        ->select('id')
                        ->where('contact_id', $id)
                        ->get();

                    $cus = DB::table('inv_clients')
                        ->select('id')
                        ->where('contact_id', $id)
                        ->get();
                }
                else {
                    $get = DB::table('inv_contacts')
                        ->select(
                            'id',
                            'company_name',
                            'name',
                            'fulladdress',
                            'phone',
                            'website',
                            'email',
                            'custom'
                        )
                        ->where('id', $id)
                        ->where('user_id', $check)
                        ->get();

                    $emp = DB::table('inv_employers')
                        ->select('id')
                        ->where('contact_id', $id)
                        ->where('user_id', $check)
                        ->get();

                    $cus = DB::table('inv_clients')
                        ->select('id')
                        ->where('contact_id', $id)
                        ->where('user_id', $check)
                        ->get();
                }

                $res = array();

                foreach($get as $g) {
                    $res[] = array(
                        'id' => $g->id,
                        'user_id' => ($valid['inv_manuser'] === 1 ? $g->user_id : $check),
                        'company_name' => $g->company_name,
                        'name' => $g->name,
                        'fulladdress' => $g->fulladdress,
                        'phone' => $g->phone,
                        'website' => $g->website,
                        'email' => $g->email,
                        'custom' => $g->custom,
                        'isEmployer' => ($emp->count() ? true : false),
                        'isCustomer' => ($cus->count() ? true : false)
                    );
                }

                return $res;
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function newContact(Request $r) { // /api/rpc/invoice/contacts/new
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_mancontact'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    $add = DB::table('inv_contacts')
                        ->insertGetId([
                            'user_id' => $r->user_id,
                            'company_name' => $r->company_name,
                            'name' => $r->name,
                            'fulladdress' => $r->fulladdress,
                            'phone' => $r->phone,
                            'website' => $r->website,
                            'email' => $r->email,
                            'custom' => $r->custom
                        ]);

                    if ($r->isCustomer) {
                        DB::table('inv_clients')
                            ->insert([
                                'user_id' => $r->user_id,
                                'contact_id' => $add
                            ]);
                    }

                    if ($r->isEmployer) {
                        DB::table('inv_employers')
                            ->insert([
                                'user_id' => $r->user_id,
                                'contact_id' => $add
                            ]);
                    }
                }
                else {
                    $add = DB::table('inv_contacts')
                        ->insertGetId([
                            'company_name' => $r->company_name,
                            'name' => $r->name,
                            'fulladdress' => $r->fulladdress,
                            'phone' => $r->phone,
                            'website' => $r->website,
                            'email' => $r->email,
                            'custom' => $r->custom
                        ]);

                    if ($r->isCustomer) {
                        DB::table('inv_clients')
                            ->insert([
                                'user_id' => $check,
                                'contact_id' => $add
                            ]);
                    }

                    if ($r->isEmployer) {
                        DB::table('inv_employers')
                            ->insert([
                                'user_id' => $check,
                                'contact_id' => $add
                            ]);
                    }
                }

                return $add;
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function editContact(Request $r) { // /api/rpc/invoice/contacts/edit
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_mancontact'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_contacts')
                        ->where('id', $r->id)
                        ->update([
                            'user_id' => $r->user_id,
                            'company_name' => $r->company_name,
                            'name' => $r->name,
                            'fulladdress' => $r->fulladdress,
                            'phone' => $r->phone,
                            'website' => $r->website,
                            'email' => $r->email,
                            'custom' => $r->custom
                        ]);

                    $emp = DB::table('inv_employers')
                        ->select('id')
                        ->where('contact_id', $r->id)
                        ->get();

                    if ($emp->count()) {
                        if (!$r->isEmployer) {
                            DB::table('inv_employers')->where('contact_id', $r->id)->delete();
                        }
                    }
                    else {
                        if ($r->isEmployer) {
                            DB::table('inv_employers')
                                ->where('id', $r->id)
                                ->insert([
                                    'user_id' => $r->user_id,
                                    'contact_id' => $r->id
                                ]);
                        }
                    }

                    $cus = DB::table('inv_clients')
                        ->select('id')
                        ->where('contact_id', $r->id)
                        ->get();

                    if ($cus->count()) {
                        if (!$r->isCustomer) {
                            DB::table('inv_clients')->where('contact_id', $r->id)->delete();
                        }
                    }
                    else {
                        if ($r->isCustomer) {
                        DB::table('inv_clients')
                                ->where('id', $r->id)
                                ->insert([
                                    'user_id' => $r->user_id,
                                    'contact_id' => $r->id
                                ]);
                        }
                    }
                }
                else {
                    DB::table('inv_contacts')
                        ->where('id', $r->id)
                        ->where('user_id', $check)
                        ->update([
                            'company_name' => $r->company_name,
                            'name' => $r->name,
                            'fulladdress' => $r->fulladdress,
                            'phone' => $r->phone,
                            'website' => $r->website,
                            'email' => $r->email,
                            'custom' => $r->custom
                        ]);

                    $emp = DB::table('inv_employers')
                        ->select('id')
                        ->where('user_id', $check)
                        ->where('contact_id', $r->id)
                        ->get();

                    if ($emp->count()) {
                        if (!$r->isEmployer) {
                            DB::table('inv_employers')->where('contact_id', $r->id)->where('user_id', $check)->delete();
                        }
                    }
                    else {
                        if ($r->isEmployer) {
                            DB::table('inv_employers')
                                ->where('id', $r->id)
                                ->where('user_id', $check)
                                ->insert([
                                    'contact_id' => $r->id
                                ]);
                        }
                    }

                    $cus = DB::table('inv_clients')
                        ->select('id')
                        ->where('user_id', $check)
                        ->where('contact_id', $r->id)
                        ->get();

                    if ($cus->count()) {
                        if (!$r->isCustomer) {
                            DB::table('inv_clients')->where('contact_id', $r->id)->where('user_id', $check)->delete();
                        }
                    }
                    else {
                        if ($r->isCustomer) {
                        DB::table('inv_clients')
                                ->where('id', $r->id)
                                ->where('user_id', $check)
                                ->insert([
                                    'contact_id' => $r->id
                                ]);
                        }
                    }
                }

                return 'Success!';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function deleteContact(Request $r) { // /api/rpc/invoice/contacts/delete
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_mancontact'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_contacts')->where('id', $r->id)->delete();
                    DB::table('inv_employers')->where('contact_id', $r->id)->delete();
                    DB::table('inv_clients')->where('contact_id', $r->id)->delete();
                }
                else {
                    DB::table('inv_contacts')->where('id', $r->id)->where('user_id', $check)->delete();
                    DB::table('inv_employers')->where('contact_id', $r->id)->where('user_id', $check)->delete();
                    DB::table('inv_clients')->where('contact_id', $r->id)->where('user_id', $check)->delete();
                }

                return 'Done.';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    // Invoices
    public function getInvoices(Request $r) { // /api/rpc/invoice/invoices/getall
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            $format = DB::table('inv_company')
                ->select('date_format')
                ->where('user_id', $check)
                ->get();

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    $get = DB::table('inv_invoices')
                        ->select(
                            'id',
                            'user_id',
                            'employer',
                            'customer',
                            'year',
                            'inv_number',
                            'revision',
                            'make_date'
                        )
                        ->get();

                    $res = array();

                    foreach ($get as $i) {
                        $usr = DB::table('inv_contacts')
                            ->select('name', 'company_name')
                            ->where('user_id', $i->user_id)
                            ->get();

                        $emp = DB::table('inv_contacts')
                            ->select('name', 'company_name')
                            ->where('id', $i->employer)
                            ->get();

                        $cus = DB::table('inv_contacts')
                            ->select('name', 'company_name')
                            ->where('id', $i->customer)
                            ->get();

                        $zero = '0000';
                        $len = strlen($i->inv_number);
                        $num = substr($zero, $len);
                        $fin = $num.$i->inv_number;

                        $res[] = array(
                            'id' => $i->id,
                            'employer' => $emp[0]->name.($emp[0]->company_name != '' ? ' ('.$emp[0]->company_name.')' : ''),
                            'customer' => $cus[0]->name.($cus[0]->company_name != '' ? ' ('.$cus[0]->company_name.')' : ''),
                            'user' => $usr[0]->name.($usr[0]->company_name != '' ? ' ('.$usr[0]->company_name.')' : ''),
                            'invoice' => $i->year.$fin.($i->revision > 0 ? '_R'.$i->revision : ''),
                            'date' => strftime($format[0]->date_format, $i->make_date)
                        );
                    }

                    return $res;
                }
                else {
                    return DB::table('inv_services')
                        ->select(
                            'id',
                            'year',
                            'inv_number',
                            'revision',
                            'make_date'
                        )
                        ->where('user_id', $check)
                        ->get();
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function getInvoice($id, Request $r) { // /api/rpc/invoice/invoices/get/id
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    $u = DB::table('inv_invoices')
                        ->select('user_id')
                        ->where('id', $id)
                        ->first();

                    $format = DB::table('inv_company')
                        ->select('date_format')
                        ->where('user_id', $u->user_id)
                        ->get();

                    $invoice = DB::table('inv_invoices')
                        ->select(
                            'id as id',
                            'user_id as user_id',
                            'employer as employer',
                            'customer as customer',
                            'year as year',
                            'inv_number as inv_number',
                            'revision as revision',
                            'make_date as make_date',
                            'invtype as invtype',
                            'tax_id as tax_id'
                        )
                        ->where('id', $id)
                        ->get();

                    $inv = array();
                    $cus = array();
                    $emp = array();
                    $com = array();
                    $desP = array();
                    $desR = array();
                    $desS = array();

                    $self = DB::table('inv_contacts')
                        ->select('id', 'company_name', 'name', 'fulladdress', 'phone', 'website', 'email', 'custom')
                        ->where('id', $u->user_id)
                        ->where('user_id', $u->user_id)
                        ->get();

                    $cus = DB::table('inv_contacts')
                        ->select('id', 'company_name', 'name', 'fulladdress', 'phone', 'website', 'email', 'custom')
                        ->where('id', $invoice[0]->customer)
                        ->where('user_id', $u->user_id)
                        ->get();

                    $emp = DB::table('inv_contacts')
                        ->select('id', 'company_name', 'name', 'fulladdress', 'phone', 'website', 'email', 'custom')
                        ->where('id', $invoice[0]->employer)
                        ->where('user_id', $u->user_id)
                        ->get();

                    $com = DB::table('inv_company')
                        ->select('id', 'name', 'compreg', 'taxnr', 'bank_number', 'bank_name', 'bank_recipient', 'logo', 'payterm', 'comment1', 'comment2')
                        ->where('user_id', $u->user_id)
                        ->get();

                    $zero = '0000';
                    $len = strlen($invoice[0]->inv_number);
                    $num = substr($zero, $len);
                    $fin = $num.$invoice[0]->inv_number;

                    $inv[] = array(
                        'id' => $id,
                        'user_id' => $u->user_id,
                        'invoice_year' => strftime('%Y', $invoice[0]->make_date),
                        'invoice_month' => strftime('%B', $invoice[0]->make_date),
                        'invoice_date' => strftime($format[0]->date_format, $invoice[0]->make_date),
                        'invoice' => $invoice[0]->year.$fin.($invoice[0]->revision > 0 ? '_R'.$invoice[0]->revision : ''),
                        'invtype' => $invoice[0]->invtype
                    );

                    $itemsP = DB::table('inv_invoice_items')
                        ->join('inv_periods', 'inv_periods.id', 'inv_invoice_items.period_id')
                        ->where('inv_invoice_items.invoice_id', $id)
                        ->where('inv_invoice_items.user_id', $u->user_id)
                        ->get(array(
                            'inv_invoice_items.id as id',
                            'inv_periods.id as period_id',
                            'inv_periods.name',
                            'inv_periods.price',
                            'inv_invoice_items.from_time',
                            'inv_invoice_items.to_time'
                        ));

                    foreach($itemsP as $j) {
                        $desP[] = array(
                            'id' => $j->id,
                            'period_id' => $j->period_id,
                            'name' => $j->name,
                            'price' => $j->price,
                            'work_date' => strftime($format[0]->date_format, $j->from_time),
                            'from_time' => strftime('%H:%M', $j->from_time),
                            'from_time_unix' => $j->from_time,
                            'from_time_js' => $j->from_time * 1000,
                            'to_time' => strftime('%H:%M', $j->to_time),
                            'to_time_unix' => $j->to_time,
                            'to_time_js' => $j->to_time * 1000
                        );
                    }

                    $itemsR = DB::table('inv_invoice_items')
                        ->join('inv_products', 'inv_products.id', 'inv_invoice_items.product_id')
                        ->where('inv_invoice_items.invoice_id', $id)
                        ->where('inv_invoice_items.user_id', $u->user_id)
                        ->get(array(
                            'inv_invoice_items.id as id',
                            'inv_products.id as product_id',
                            'inv_products.name',
                            'inv_products.price',
                            'inv_invoice_items.from_time',
                            'inv_invoice_items.to_time'
                        ));

                    foreach($itemsR as $j) {
                        $desR[] = array(
                            'id' => $j->id,
                            'product_id' => $j->product_id,
                            'name' => $j->name,
                            'price' => $j->price,
                            'work_date' => strftime($format[0]->date_format, $j->from_time),
                            'from_time' => strftime('%H:%M', $j->from_time),
                            'from_time_unix' => $j->from_time,
                            'from_time_js' => $j->from_time * 1000,
                            'to_time' => strftime('%H:%M', $j->to_time),
                            'to_time_unix' => $j->to_time,
                            'to_time_js' => $j->to_time * 1000
                        );
                    }

                    $itemsS = DB::table('inv_invoice_items')
                        ->join('inv_services', 'inv_services.id', 'inv_invoice_items.service_id')
                        ->where('inv_invoice_items.invoice_id', $id)
                        ->where('inv_invoice_items.user_id', $u->user_id)
                        ->get(array(
                            'inv_invoice_items.id as id',
                            'inv_services.id as service_id',
                            'inv_services.name',
                            'inv_services.price',
                            'inv_invoice_items.from_time',
                            'inv_invoice_items.to_time'
                        ));

                    foreach($itemsS as $j) {
                        $desS[] = array(
                            'id' => $j->id,
                            'service_id' => $j->service_id,
                            'name' => $j->name,
                            'price' => $j->price,
                            'work_date' => strftime($format[0]->date_format, $j->from_time),
                            'from_time' => strftime('%H:%M', $j->from_time),
                            'from_time_unix' => $j->from_time,
                            'from_time_js' => $j->from_time * 1000,
                            'to_time' => strftime('%H:%M', $j->to_time),
                            'to_time_unix' => $j->to_time,
                            'to_time_js' => $j->to_time * 1000
                        );
                    }

                    return array(
                        'invoice' => $inv,
                        'user' => $self,
                        'company' => $com,
                        'employer' => $emp,
                        'customer' => $cus,
                        'periods' => $desP,
                        'products' => $desR,
                        'services' => $desS
                    );
                }
                else {
                    $format = DB::table('inv_company')
                        ->select('date_format')
                        ->where('user_id', $check)
                        ->get();

                    $invoice = DB::table('inv_invoices')
                        ->select(
                            'id as id',
                            'user_id as user_id',
                            'employer as employer',
                            'customer as customer',
                            'year as year',
                            'inv_number as inv_number',
                            'revision as revision',
                            'make_date as make_date',
                            'invtype as invtype',
                            'tax_id as tax_id'
                        )
                        ->where('id', $id)
                        ->where('user_id', $check)
                        ->get();

                    $inv = array();
                    $cus = array();
                    $emp = array();
                    $com = array();
                    $desP = array();
                    $desR = array();
                    $desS = array();

                    $self = DB::table('inv_contacts')
                        ->select('id', 'company_name', 'name', 'fulladdress', 'phone', 'website', 'email', 'custom')
                        ->where('id', $check)
                        ->where('user_id', $check)
                        ->get();

                    $cus = DB::table('inv_contacts')
                        ->select('id', 'company_name', 'name', 'fulladdress', 'phone', 'website', 'email', 'custom')
                        ->where('id', $invoice[0]->customer)
                        ->where('user_id', $check)
                        ->get();

                    $emp = DB::table('inv_contacts')
                        ->select('id', 'company_name', 'name', 'fulladdress', 'phone', 'website', 'email', 'custom')
                        ->where('id', $invoice[0]->employer)
                        ->where('user_id', $check)
                        ->get();

                    $com = DB::table('inv_company')
                        ->select('id', 'name', 'compreg', 'taxnr', 'bank_number', 'bank_name', 'bank_recipient', 'logo', 'payterm', 'comment1', 'comment2')
                        ->where('user_id', $check)
                        ->get();

                    $zero = '0000';
                    $len = strlen($invoice[0]->inv_number);
                    $num = substr($zero, $len);
                    $fin = $num.$invoice[0]->inv_number;

                    $inv[] = array(
                        'id' => $id,
                        'user_id' => $u->user_id,
                        'invoice_year' => strftime('%Y', $invoice[0]->make_date),
                        'invoice_month' => strftime('%B', $invoice[0]->make_date),
                        'invoice_date' => strftime($format[0]->date_format, $invoice[0]->make_date),
                        'invoice' => $invoice[0]->year.$fin.($invoice[0]->revision > 0 ? '_R'.$invoice[0]->revision : ''),
                        'invtype' => $invoice[0]->invtype
                    );

                    $itemsP = DB::table('inv_invoice_items')
                        ->join('inv_periods', 'inv_periods.id', 'inv_invoice_items.period_id')
                        ->where('inv_invoice_items.invoice_id', $id)
                        ->where('inv_invoice_items.user_id', $check)
                        ->get(array(
                            'inv_invoice_items.id as id',
                            'inv_periods.id as period_id',
                            'inv_periods.name',
                            'inv_periods.price',
                            'inv_invoice_items.from_time',
                            'inv_invoice_items.to_time'
                        ));

                    foreach($itemsP as $j) {
                        $desP[] = array(
                            'id' => $j->id,
                            'period_id' => $j->period_id,
                            'name' => $j->name,
                            'price' => $j->price,
                            'work_date' => strftime($format[0]->date_format, $j->from_time),
                            'from_time' => strftime('%H:%M', $j->from_time),
                            'from_time_unix' => $j->from_time,
                            'from_time_js' => $j->from_time * 1000,
                            'to_time' => strftime('%H:%M', $j->to_time),
                            'to_time_unix' => $j->to_time,
                            'to_time_js' => $j->to_time * 1000
                        );
                    }

                    $itemsR = DB::table('inv_invoice_items')
                        ->join('inv_products', 'inv_products.id', 'inv_invoice_items.product_id')
                        ->where('inv_invoice_items.invoice_id', $id)
                        ->where('inv_invoice_items.user_id', $check)
                        ->get(array(
                            'inv_invoice_items.id as id',
                            'inv_products.id as product_id',
                            'inv_products.name',
                            'inv_products.price',
                            'inv_invoice_items.from_time',
                            'inv_invoice_items.to_time'
                        ));

                    foreach($itemsR as $j) {
                        $desR[] = array(
                            'id' => $j->id,
                            'product_id' => $j->product_id,
                            'name' => $j->name,
                            'price' => $j->price,
                            'work_date' => strftime($format[0]->date_format, $j->from_time),
                            'from_time' => strftime('%H:%M', $j->from_time),
                            'from_time_unix' => $j->from_time,
                            'from_time_js' => $j->from_time * 1000,
                            'to_time' => strftime('%H:%M', $j->to_time),
                            'to_time_unix' => $j->to_time,
                            'to_time_js' => $j->to_time * 1000
                        );
                    }

                    $itemsS = DB::table('inv_invoice_items')
                        ->join('inv_services', 'inv_services.id', 'inv_invoice_items.service_id')
                        ->where('inv_invoice_items.invoice_id', $id)
                        ->where('inv_invoice_items.user_id', $check)
                        ->get(array(
                            'inv_invoice_items.id as id',
                            'inv_services.id as service_id',
                            'inv_services.name',
                            'inv_services.price',
                            'inv_invoice_items.from_time',
                            'inv_invoice_items.to_time'
                        ));

                    foreach($itemsS as $j) {
                        $desS[] = array(
                            'id' => $j->id,
                            'service_id' => $j->service_id,
                            'name' => $j->name,
                            'price' => $j->price,
                            'work_date' => strftime($format[0]->date_format, $j->from_time),
                            'from_time' => strftime('%H:%M', $j->from_time),
                            'from_time_unix' => $j->from_time,
                            'from_time_js' => $j->from_time * 1000,
                            'to_time' => strftime('%H:%M', $j->to_time),
                            'to_time_unix' => $j->to_time,
                            'to_time_js' => $j->to_time * 1000
                        );
                    }

                    return array(
                        'invoice' => $inv,
                        'user' => $self,
                        'company' => $com,
                        'employer' => $emp,
                        'customer' => $cus,
                        'periods' => $desP,
                        'products' => $desR,
                        'services' => $desS
                    );
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function newInvoiceItem(Request $r) { // /api/rpc/invoice/invoices/newitem
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_invoice_items')
                        ->insert([
                            'user_id' => $r->user_id,
                            'invoice_id' => $r->inv,
                            'service_id' => $r->service_id,
                            'period_id' => $r->period_id,
                            'product_id' => $r->product_id,
                            'from_time' => $r->from_time,
                            'to_time' => $r->to_time
                        ]);
                }
                else {
                    DB::table('inv_invoice_items')
                        ->insert([
                            'user_id' => $check,
                            'invoice_id' => $r->inv,
                            'service_id' => $r->service_id,
                            'period_id' => $r->period_id,
                            'product_id' => $r->product_id,
                            'from_time' => $r->from_time,
                            'to_time' => $r->to_time
                        ]);
                }

                return 'Success!';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function newInvoice(Request $r) { // /api/rpc/invoice/invoices/new
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    $lastInv = DB::table('inv_invoices')
                        ->where('year', date('Y'))
                        ->where('user_id', $r->user_id)
                        ->count();

                    $add = DB::table('inv_invoices')
                        ->insertGetId([
                            'user_id' => $r->user_id,
                            'customer' => $r->customer,
                            'employer' => $r->employer,
                            'year' => date('Y'),
                            'inv_number' => $lastInv + 1,
                            'revision' => 0,
                            'make_date' => time(),
                            'invtype' => $r->invtype,
                            'tax_id' => 0
                        ]);
                }
                else {
                    $lastInv = DB::table('inv_invoices')
                        ->where('year', date('Y'))
                        ->where('user_id', $check)
                        ->count();

                    $add = DB::table('inv_invoices')
                        ->insertGetId([
                            'user_id' => $check,
                            'customer' => $r->customer,
                            'employer' => $r->employer,
                            'year' => date('Y'),
                            'inv_number' => $lastInv + 1,
                            'revision' => 0,
                            'make_date' => time(),
                            'invtype' => $r->invtype,
                            'tax_id' => 0
                        ]);
                }

                return $add;
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function editInvoiceItem(Request $r) { // /api/rpc/invoice/invoices/edititem
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_invoice_items')
                        ->where('id', $r->id)
                        ->where('invoice_id', $r->inv)
                        ->update([
                            'service_id' => $r->service_id,
                            'period_id' => $r->period_id,
                            'product_id' => $r->product_id,
                            'from_time' => $r->from_time,
                            'to_time' => $r->to_time
                       ]);
                }
                else {
                    DB::table('inv_invoice_items')
                        ->where('id', $r->id)
                        ->where('user_id', $check)
                        ->where('invoice_id', $r->inv)
                        ->update([
                            'service_id' => $r->service_id,
                            'period_id' => $r->period_id,
                            'product_id' => $r->product_id,
                            'from_time' => $r->from_time,
                            'to_time' => $r->to_time
                       ]);
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function editInvoice(Request $r) { // /api/rpc/invoice/invoices/edit
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                $rev = DB::table('inv_invoices')
                    ->select('revision')
                    ->where('id', $r->id)
                    ->get();

                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_invoices')
                        ->where('id', $r->id)
                        ->update([
                            'user_id' => $r->user_id,
                            'customer' => $r->customer,
                            'employer' => $r->employer,
                            'invtype' => $r->invtype,
                            'tax_id' => 0,
                            'revision' => $rev[0]->revision + 1
                       ]);
                }
                else {
                    DB::table('inv_invoices')
                        ->where('id', $r->id)
                        ->where('user_id', $check)
                        ->update([
                            'customer' => $r->customer,
                            'employer' => $r->employer,
                            'invtype' => $r->invtype,
                            'tax_id' => 0,
                            'revision' => $rev[0]->revision + 1
                       ]);
                }

                return 'Success!';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function deleteInvoice(Request $r) { // /api/rpc/invoice/invoices/delete
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_invoices')->where('id', $r->id)->delete();
                }
                else {
                    DB::table('inv_invoices')->where('id', $r->id)->where('user_id', $check)->delete();
                }

                return 'Done.';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    // Periods
    public function getPeriods(Request $r) { // /api/rpc/invoice/periods/getall
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    return DB::table('inv_periods')
                        ->select(
                            'id',
                            'user_id',
                            'name',
                            'price',
                            'intervals',
                            'period'
                        )
                        ->get();

                }
                else {
                    return DB::table('inv_periods')
                        ->select(
                            'id',
                            'name',
                            'price',
                            'intervals',
                            'period'
                        )
                        ->where('user_id', $check)
                        ->get();
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function getPeriod($id, Request $r) { // /api/rpc/invoice/periods/get/id
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    return DB::table('inv_periods')
                        ->select(
                            'id',
                            'user_id',
                            'name',
                            'price',
                            'intervals',
                            'period'
                        )
                        ->where('id', $id)
                        ->get();
                }
                else {
                    return DB::table('inv_periods')
                        ->select(
                            'id',
                            'name',
                            'price',
                            'intervals',
                            'period'
                        )
                        ->where('id', $id)
                        ->where('user_id', $check)
                        ->get();
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function newPeriod(Request $r) { // /api/rpc/invoice/periods/new
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    $add = DB::table('inv_periods')
                        ->insertGetId([
                            'user_id' => $r->user_id,
                            'name' => $r->name,
                            'price' => $r->price,
                            'intervals' => $r->intervals,
                            'period' => $r->period
                        ]);
                }
                else {
                    $add = DB::table('inv_periods')
                        ->insertGetId([
                            'user_id' => $check,
                            'name' => $r->name,
                            'price' => $r->price,
                            'intervals' => $r->intervals,
                            'period' => $r->period
                        ]);
                }

                return $add;
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function editPeriod(Request $r) { // /api/rpc/invoice/periods/edit
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_periods')
                        ->where('id', $r->id)
                        ->update([
                            'user_id' => $r->user_id,
                            'name' => $r->name,
                            'price' => $r->price,
                            'intervals' => $r->intervals,
                            'period' => $r->period
                       ]);
                }
                else {
                    DB::table('inv_periods')
                        ->where('id', $r->id)
                        ->where('user_id', $check)
                        ->update([
                            'name' => $r->name,
                            'price' => $r->price,
                            'intervals' => $r->intervals,
                            'period' => $r->period
                       ]);
                }

                return 'Success!';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function deletePeriod(Request $r) { // /api/rpc/invoice/periods/delete
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_periods')->where('id', $r->id)->delete();
                }
                else {
                    DB::table('inv_periods')->where('id', $r->id)->where('user_id', $check)->delete();
                }

                return 'Done.';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    // Products
    public function getProducts(Request $r) { // /api/rpc/invoice/products/getall
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    return DB::table('inv_products')
                        ->select(
                            'id',
                            'user_id',
                            'name',
                            'price'
                        )
                        ->get();

                }
                else {
                    return DB::table('inv_products')
                        ->select(
                            'id',
                            'name',
                            'price'
                        )
                        ->where('user_id', $check)
                        ->get();
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function getProduct($id, Request $r) { // /api/rpc/invoice/products/get/id
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    $get = DB::table('inv_products')
                        ->select(
                            'id',
                            'user_id',
                            'name',
                            'price'
                        )
                        ->where('id', $id)
                        ->get();

                    return $get;
                }
                else {
                    $get = DB::table('inv_products')
                        ->select(
                            'id',
                            'name',
                            'price'
                        )
                        ->where('id', $id)
                        ->where('user_id', $check)
                        ->get();

                    return $get;
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function newProduct(Request $r) { // /api/rpc/invoice/products/new
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    $add = DB::table('inv_products')
                        ->insertGetId([
                            'user_id' => $r->user_id,
                            'name' => $r->name,
                            'price' => $r->price
                        ]);
                }
                else {
                    $add = DB::table('inv_products')
                        ->insertGetId([
                            'user_id' => $check,
                            'name' => $r->name,
                            'price' => $r->price
                        ]);
                }

                return $add;
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function editProduct(Request $r) { // /api/rpc/invoice/products/edit
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_products')
                        ->where('id', $r->id)
                        ->update([
                            'user_id' => $r->user_id,
                            'name' => $r->name,
                            'price' => $r->price
                       ]);
                }
                else {
                    DB::table('inv_products')
                        ->where('id', $r->id)
                        ->where('user_id', $check)
                        ->update([
                            'name' => $r->name,
                            'price' => $r->price
                       ]);
                }

                return 'Success!';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function deleteProduct(Request $r) { // /api/rpc/invoice/products/delete
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_products')->where('id', $r->id)->delete();
                }
                else {
                    DB::table('inv_products')->where('id', $r->id)->where('user_id', $check)->delete();
                }

                return 'Done.';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    // Services
    public function getServices(Request $r) { // /api/rpc/invoice/services/getall
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    return DB::table('inv_services')
                        ->select(
                            'id',
                            'user_id',
                            'name',
                            'price'
                        )
                        ->get();

                }
                else {
                    return DB::table('inv_services')
                        ->select(
                            'id',
                            'name',
                            'price'
                        )
                        ->where('user_id', $check)
                        ->get();
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function getService($id, Request $r) { // /api/rpc/invoice/services/get/id
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    return DB::table('inv_services')
                        ->select('*')
                        ->where('id', $id)
                        ->get();
                }
                else {
                    return DB::table('inv_services')
                        ->select('*')
                        ->where('id', $id)
                        ->where('user_id', $check)
                        ->get();
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function newService(Request $r) { // /api/rpc/invoice/services/new
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    $add = DB::table('inv_services')
                        ->insertGetId([
                            'user_id' => $r->user_id,
                            'name' => $r->name,
                            'price' => $r->price
                        ]);
                }
                else {
                    $add = DB::table('inv_services')
                        ->insertGetId([
                            'user_id' => $check,
                            'name' => $r->name,
                            'price' => $r->price
                        ]);
                }

                return $add;
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function editService(Request $r) { // /api/rpc/invoice/services/edit
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_services')
                        ->where('id', $r->id)
                        ->update([
                            'user_id' => $r->user_id,
                            'name' => $r->name,
                            'price' => $r->price
                       ]);
                }
                else {
                    DB::table('inv_services')
                        ->where('id', $r->id)
                        ->where('user_id', $check)
                        ->update([
                            'user_id' => $check,
                            'name' => $r->name,
                            'price' => $r->price
                       ]);
                }

                return 'Success!';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function deleteService(Request $r) { // /api/rpc/invoice/services/delete
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_services')->where('id', $r->id)->delete();
                }
                else {
                    DB::table('inv_services')->where('id', $r->id)->where('user_id', $check)->delete();
                }

                return 'Done.';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    // Tax
    public function getTaxes(Request $r) { // /api/rpc/invoice/taxes/getall
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    return DB::table('inv_taxes')
                        ->select(
                            'id',
                            'user_id',
                            'name',
                            'percentage'
                        )
                        ->get();

                }
                else {
                    return DB::table('inv_taxes')
                        ->select(
                            'id',
                            'name',
                            'percentage'
                        )
                        ->where('user_id', $check)
                        ->get();
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function getTax($id, Request $r) { // /api/rpc/invoice/taxes/get/id
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    return DB::table('inv_taxes')
                        ->select('*')
                        ->where('id', $id)
                        ->get();
                }
                else {
                    return DB::table('inv_taxes')
                        ->select('*')
                        ->where('id', $id)
                        ->where('user_id', $check)
                        ->get();
                }
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function newTax(Request $r) { // /api/rpc/invoice/taxes/new
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    $add = DB::table('inv_taxes')
                        ->insertGetId([
                            'user_id' => $r->user_id,
                            'name' => $r->name,
                            'percentage' => $r->percentage
                        ]);
                }
                else {
                    $add = DB::table('inv_taxes')
                        ->insertGetId([
                            'user_id' => $check,
                            'name' => $r->name,
                            'percentage' => $r->percentage
                        ]);
                }

                return $add;
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function editTax(Request $r) { // /api/rpc/invoice/taxes/edit
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_taxes')
                        ->where('id', $r->id)
                        ->update([
                            'user_id' => $r->user_id,
                            'name' => $r->name,
                            'percentage' => $r->percentage
                       ]);
                }
                else {
                    DB::table('inv_taxes')
                        ->where('id', $r->id)
                        ->where('user_id', $check)
                        ->update([
                            'user_id' => $check,
                            'name' => $r->name,
                            'percentage' => $r->percentage
                       ]);
                }

                return 'Success!';
            }
            else {
                return 'Permission denied.';
            }
        }
    }

    public function deleteTax(Request $r) { // /api/rpc/invoice/taxes/delete
        $check = $this->objAuth->checkLegit($r->kero_token);

        if ($check == 0) {
            return 'Err!';
        }
        else {
            $valid = $this->objAuth->getPermissions($r->kero_token);

            if ($valid['inv_makeinvoice'] == 1) {
                if ($valid['inv_manuser'] == 1) {
                    DB::table('inv_taxes')->where('id', $r->id)->delete();
                }
                else {
                    DB::table('inv_taxes')->where('id', $r->id)->where('user_id', $check)->delete();
                }

                return 'Done.';
            }
            else {
                return 'Permission denied.';
            }
        }
    }
}
