<?php
/**
 * Created by PhpStorm.
 * User: jmartinez
 * Date: 5/10/2017
 * Time: 2:56 PM
 */

namespace Hilco\Models;

class PromotionSalesRepTrigger extends PromotionTriggerDetails {
    protected $table = 'PromotionSalesRepTriggers';

    public function salesReps() {
        return $this->belongsToMany(SalesRep::class, 'PromotionSalesRepTrigger_SalesRep', 'promotionsalesreptrigger_id', 'salesrep_id')
                    ->wherePivot('deleted_at', '=', DB::raw(0))
                    ->withPivot(['excluded']);
    }

    public function isTriggered(Customer $soldToCustomer, $webCartItems, $distinctPartInfo = []) {
        $salesRepForActiveCustomer = $soldToCustomer->activeSegment->primarySalesRep;
        $salesRep = $this->salesReps()->wherePivot('salesrep_id', $salesRepForActiveCustomer->id)->first();
        return !is_null($salesRep);
    }

    public function postTriggerOperation($soldToCustomer)
    {
        // TODO: Implement postTriggerOperation() method.
    }
}