<?php

namespace App\Classes\Service;


use App\Booking;
use App\ChinaBankSlip;
use App\Invoice;
use App\SupplierBooking;
use App\User;
use App\UserBankSlip;
use Carbon\Carbon;

class BulkBookings
{
    /** @var Carbon */
    protected $date;
    /** @var int */
    protected $referenceNumber;
    /** @var float */
    protected $customerDeposit;
    /** @var string */
    protected $marking;
    /** @var float */
    protected $vendorDeposit;
    /** @var float */
    protected $bookingAmount;

    /**
     * BulkBookings constructor.
     * @param Carbon $date
     * @param int $referenceNumber
     * @param float $customerDeposit
     * @param string $marking
     * @param float $vendorDeposit
     * @param float $bookingAmount
     */
    public function __construct(Carbon $date, int $referenceNumber, float $customerDeposit, string $marking, float $vendorDeposit, float $bookingAmount)
    {
        $this->date = $date;
        $this->referenceNumber = $referenceNumber;
        $this->customerDeposit = floatval($customerDeposit);
        $this->marking = $marking;
        $this->vendorDeposit = $vendorDeposit;
        $this->bookingAmount = $bookingAmount;
    }


    public function execute(){
        $booking = new Booking();
        $booking->id = $this->referenceNumber;
        $booking->term = 'x1_cash';
        $booking->account_name = '';
        $booking->account_num = '';
        $booking->bank_name = '';
        $booking->bank_branch = '';
        $booking->rate_id = 1;
        $booking->amount = $this->bookingAmount;
        $booking->user_id = $this->userID();
        $booking->bia = $this->customerDeposit;
        $booking->rmb_book_amount = $this->bookingAmount;
        $booking->verification_status = true;
        $booking->rate = $this->customerRate();
        $booking->status = 7;
        $booking->admin_status = 7;
        $booking->payment_for = 'Full Payment';
        $booking->created_at = $this->date;
        $booking->save();

        $supplierBooking = new SupplierBooking();
        $supplierBooking->rate = $this->vendorRate();
        $supplierBooking->rebate = 0;
        $supplierBooking->amountInRMB= $this->bookingAmount;
        $supplierBooking->supplier_id= 2;
        $supplierBooking->salestax_amount = 0;
        $supplierBooking->booking_id= $this->referenceNumber;
        $supplierBooking->amount_in_myr = $this->vendorDeposit;
        $supplierBooking->tax_rate = 1;
        $supplierBooking->amount_after_tax = $this->vendorDeposit;
        $supplierBooking->save();

        $bankSlip = new UserBankSlip();
        $bankSlip->transfer_amount = $this->customerDeposit;
        $bankSlip->booking_id = $this->referenceNumber;
        $bankSlip->is_approve = true;
        $bankSlip->save();

        $chinaBankSlip = new ChinaBankSlip();
        $chinaBankSlip->china_bank_slip_path = '';
        $chinaBankSlip->actual_transfer_amount = $this->vendorDeposit;
        $chinaBankSlip->booking_id = $this->referenceNumber;
        $chinaBankSlip->save();

        $invoice = new Invoice();
        $invoice->invoice_path = '';
        $invoice->amount = 0;
        $invoice->booking_id = $this->referenceNumber;
        $invoice->save();

    }

    private function userID(){
        if($this->marking === "721PCV"){
            $user = User::where('marking', 'SRW/CIEF/'.$this->marking)->first();
        } else {
            $user = User::where('marking', 'CIEF/'.$this->marking)->first();
        }

        return $user->id;
    }

    private function customerRate(){
        return $this->currencyRate(($this->bookingAmount + (($this->bookingAmount / 100) * 2)) / $this->customerDeposit);
    }

    private function vendorRate(){
        return $this->currencyRate($this->bookingAmount / $this->vendorDeposit);
    }

    private function currencyRate($rate){
        return floor($rate * 1000) / 1000;
    }
}