<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class PaymentDetail extends Model
{
    public function __construct()
    {
           
       $this->setCondition(['is_recycle' => '0']);

    }

    public function setCondition($condition = [])
    {
        $this->generic_condition = $condition;
    }

   



    public function getCondition()
    {
        return $this->generic_condition;
    }
    
    public function bulkStorePaymentDetail($object)
    {
      

      return DB::transaction(function () use ($object)  {

            return PaymentDetail::insert($object);

        });
    }

    public function getPaymentDetailByPayment($payment)
    {
    	$condition = $this->getCondition() + ['payment_id' => $payment];

    	return PaymentDetail::where($condition)->orderBy('id','asc')->get();
    }

    public function removePaymentDetailByPayment($payment)
    {
    	return DB::transaction(function () use ($payment)  {

    			$installment = new Installment;

    			$payment_detail_list = $this->getPaymentDetailByPayment($payment);

                foreach ($payment_detail_list as $rows) 
                {
                    $installment->updateInstallmentByPayment($rows->installment_id,0,$rows->amount);

                    $rows->is_recycle   = '1';

                    $rows->save();
                }
        });
    	
    }
}
