HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers85/equichecker.com/app/KommaApp/Debtors/Kms/DebtorRepository.php
<?php
/**
 *
 *
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma Mediadesign
 */

namespace KommaApp\Debtors\Kms;

use Illuminate\Mail\Message;
use Illuminate\Support\Collection;
use KommaApp\Kms\Core\Kms;
use KommaApp\Kms\Core\KmsRepository;
use KommaApp\Debtors\Models\Debtor;
use KommaApp\Kms\SidebarListItem;

class DebtorRepository extends KmsRepository
{

    private $debtorService;


    public function __construct(Kms $kms, DebtorService $debtorService)
    {
        parent::__construct($kms);

        $this->debtorService = $debtorService;
    }


    /**
     * This method gets an Debtor model or null
     * When $debtor is null, call newModel()
     * This is Called in KmsSection@loadEntity.
     *
     * @param $debtor Debtor or null
     * @return Debtor
     */
    public function getModel($debtor)
    {
        //Check if debtor is null, return new model
        if ($debtor == null) return $this->newModel();

        //Set the extra form fields, eg. title, thumbnail
        $this->setExtraModelFields($debtor);


        return $debtor;
    }

    /**
     * This method will set extra modelFields.
     * eg title and thumbnail for the form.
     *
     * @param $model
     */
    public function setExtraModelFields(&$model, $new = false)
    {
        //Set title
        $model->title = $model->compamy_name;

        //Set the thumbnail
        $this->setThumbnail($model);

    }

    /**
     * Set the thumbnail for the model.
     *
     * @param $model
     */
    public function setThumbnail(&$model)
    {
        //Set the thumbnail
        $model->thumbnail = ['image_url' => '/images/kms/structure/users.png'];
    }

    /**
     * This method will generate a new Debtor container.
     * It is only used to create the new debtor Form.
     *
     * @return Debtor
     */
    public function newModel()
    {
        $debtor = new Debtor();

        //Set the thumbnail
        $this->setThumbnail($debtor);

        return $debtor;
    }


    /**
     * This method will get all the models.
     * And add these to the sidebarList.
     *
     * @return sidebarList
     */
    public function getModels()
    {
        //Load the Debtors
        $debtors = Debtor::all();

        //Create an empty sidebarList
        $sidebarList = [];
        //Loop trough the shippingCosts
        foreach ($debtors as $debtor) {
            //New SidebarListItem
            $sidebarListItem = new SidebarListItem();
            //Set the values for the sidebar
            $sidebarListItem->setId($debtor->id);
            $sidebarListItem->setStatus($debtor->active);
            //set the name as 'Shipping to {country name}'

            if ($debtor->debtors_type == '1') {
                $sidebarListItem->setName('B- '.$debtor->company_name . ' ' . $debtor->office_country);

            } else {
                $sidebarListItem->setName('P- '.$debtor->last_name . ' ' . $debtor->initials . ' ' . $debtor->private_country);
            }

            $sidebarListItem->setThumbnail($debtor->id);
            $sidebarList[] = $sidebarListItem;
        }
        return $sidebarList;
    }


    /**
     * This method will save the Debtor.
     *
     * @param      $input
     * @param null $debtor KommaApp\Debtors\Models\Debtor
     * @return Debtor id
     */
    public function saveModel($input, $debtor = null)
    {

        $debtor = $this->debtorService->saveDebtor($input, $debtor);

        return $debtor->id;

    }

    public function destroyModel($debtor)
    {
        $debtor->delete();
    }

    public function getOrdersByDebtorId($languageId, $debtorId)
    {
        $records = [];
        if (is_numeric($debtorId)) {
            $records = Order::where('debtor_id', $debtorId)->get();

        }
        return $records;
    }

    private function sendActivationEmail($debtor)
    {
        $debtor->active = 0;
        $debtor->save();

        //user found, so create an send activate email
        \Password::debtor()->sendResetLink(['email' => $debtor->email], function (Message $message) {
            $message->subject(\Lang::get('auth.password-activate-email.subject'));
        });
    }


}