<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
 
class Transaction extends Model
{
    use SoftDeletes, BaseModel;
    
    protected $table = 'transactions';
    protected $primaryKey = 'transaction_id';
    protected $fillable =	[
      'transaction_id', // primary
      'site_id',
      'transaction_invoice_code', 
      'customer_id', 
      'customer_name',
      'transaction_dp', 
      'total_transaction', 
      'payment_status', 
      'created_by',
      'updated_by',
      'tax_rate',
      'tax_value',
      'cart_service_id',
      'price_before_tax',
      'staff_id',
      'staff_name'
    ];
    
    protected static $rules = [
    ];

    protected $appends = [
        'total_transaction_before_discount',
        'payment_amount_suggestion',
        'transaction_status'
    ];

    public function paymentHistory() {
        return $this->hasOne(TransactionPaymentHistory::class, 'transaction_id');
    }
    public function orderContactInformation() {
        return $this->hasOne(OrderContactInformation::class, 'transaction_id');
    }
    public function orderOutletInformation() {
        return $this->hasOne(OrderOutletInformation::class, 'transaction_id');
    }

    public function orders(){
      return $this->hasMany('App\Http\Models\TransactionItem','transaction_id','transaction_id');
    }
    public function customer_signature(){
      return $this->hasOne('App\Http\Models\GeneralCheckupSignature','cart_service_id','cart_service_id');
    }

    public function getTotalTransactionBeforeDiscountAttribute()
    {
        return number_format($this->total_transaction + $this->voucher_value, 2, '.','');
    }

    public function getTransactionStatusAttribute()
   {
        if($this->payment_status == 3){
            return "Finished";
        } elseif($this->payment_status == 2 || $this->payment_status == 1){
            return "On Going";
        }elseif($this->payment_status == 0 ){
            return "Canceled";
        }
   }
    // public function getPaymentAmountSuggestionAttribute()
    // {
    //     $amount = 0;
    //     foreach ($this->orders as $order) {
    //         foreach ($order->items as $item) {
    //             if ($item->tax_rate > 0) {
    //                 $amountX = (int) (($item->price_before_tax + ($item->price_before_tax * $item->tax_rate)) - $item->item_disc_value);
    //                 $x = substr($amountX, -3);
    //                 $x = round($x/100)*100;
    //                 $x = $x * $item->item_qty ;
    //                 $result = substr($amountX, strlen($amountX)*-1, strlen($amountX)-3) * 1000;
    //                 $amount += $result + $x ;
    //             } else {
    //                 $amount += $item->price_before_tax * $item->item_qty;
    //             }
    //         }
    //         return number_format(roundCurrency2($amount), 2, '.','');
    //     }      
    // }

    //no need tax
    public function getPaymentAmountSuggestionAttribute()
    {
 return number_format(roundCurrency2($this->total_transaction), 2, '.','');
       

    }
    // include tax perhitungan
    // public function getPaymentAmountSuggestionAttribute()
    // {
    //     $amount = 0;
    //     foreach ($this->orders as $order) {
    //         foreach ($order->items as $item) {
    //             if ($item->tax_rate > 0) {
    //                 $amountX = (int) (($item->price_before_tax + ($item->price_before_tax * $item->tax_rate)) - $item->item_disc_value);
    //                 $x = substr($amountX, -3);

    //                 $x = round($x/100)*100;

    //                 $x = $x  ;

    //                 $result = substr($amountX, strlen($amountX)*-1, strlen($amountX)-3) * 1000 ;
    //                 $amount += (($result + $x) * $item->item_qty );
    //                 // $amount += (($result + $x) * $item->item_qty ) - $item->trade_in_amount;
    //             } else {
    //                 // $amount += ($item->price_before_tax * $item->item_qty) - $item->trade_in_amount;
    //                 $amount += ($item->price_before_tax * $item->item_qty);
    //             }
    //         }
    //         return number_format(roundCurrency2($amount), 2, '.','');
    //     }      
    // }

    //DOMO
    // public function getPaymentAmountSuggestionAttribute()
    // {
    //     $amount = 0;
    //     foreach ($this->orders as $order) {
    //         foreach ($order->items as $item) {
    //             if ($item->tax_rate > 0) {
    //                 $amountX = (int) (($item->price_before_tax + ($item->price_before_tax * $item->tax_rate)) - $item->item_disc_value);
    //                 // $x = substr($amountX, -3);
    //                 $amountX=$amountX *  $item->item_qty;
              
    //                 $x = substr($amount, -2);
    //                 if($x >= 51){
    //                     $x = ceil($x/100)*100;
    //                 } else {
    //                     $x = floor($x/100)*100;
    //                 }
            
    //                 $result = substr($amountX, strlen($amountX)*-1, strlen($amountX)-3) * 1000;
    //                 $amount += $result + $x;
    //             } else {
    //                 $amount += $item->price_before_tax * $item->item_qty;
    //             }
    //         }
    //     }
    //     return number_format($amount, 2, '.','');
    // }

    
}

