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/DebtorService.php
<?php

/**
 * Short description for the file.
 *
 * @author      Tim Van Samang <timvansamang@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace KommaApp\Debtors\Kms;

use KommaApp\Countries\CountryService;
use KommaApp\Debtors\Models\Debtor;
use Maatwebsite\Excel\Excel;
use KommaApp\Debtors\DebtorService as baseDebtorService;

class DebtorService extends baseDebtorService
{

    /**
     * Maatwebsite\Excel\Excel class
     *
     * @var Excel
     */
    private $excel;

    private $countryService;

    /**
     * DebtorService constructor.
     * @param Excel $excel
     */
    public function __construct(Excel $excel, CountryService $countryService)
    {
        $this->excel = $excel;
        $this->countryService = $countryService;
    }

    /**
     * This method saves a Debtor
     * When needed it will create a new
     * It will prepare all the fields
     *
     * @param $input
     * @param null $debtor
     * @return Debtor|null
     */
    public function saveDebtor($input, $debtor = null)
    {

        //Check if the debtor is null
        if ($debtor == null) {
            //Create a new debtor
            $debtor = new Debtor();
        }

        //Prepare the fields
        $this->prepareDebtor($debtor, $input);

        $debtor->save();
        return $debtor;
    }


    /**
     * This method prepares the debtor model
     * It will convert the input to the desired format.
     * and return the Debtor by reference
     *
     * @param $debtor
     * @param $input
     */
    private function prepareDebtor(&$debtor, $input)
    {
        /**
         * Loop trough the input and add these fields to the model
         */
        foreach ($input as $key => $value) {
            if(str_contains($key, 'label')) continue;
            $debtor->$key = $value;
        }
        $debtor->debtors_code = $this->makeUnique('debtors_code', $debtor->debtors_code, $debtor->id);

        $debtor->hashed_debtors_code = $this->makeUnique('hashed_debtors_code', str_slug(\Hash::make($debtor->debtors_code)), $debtor->id);

        $debtor->company_name_search = $this->cleanText($debtor->company_name);
        $debtor->last_name_search = $this->cleanText($debtor->last_name);
        $debtor->office_street_search = $this->cleanText($debtor->office_street);
        $debtor->post_street_search = $this->cleanText($debtor->post_street);
        $debtor->private_street_search = $this->cleanText($debtor->private_street);


        $debtor->company_phone_search = $this->prepareTelephoneNumber($debtor->company_phone);
        $debtor->office_phone_search = $this->prepareTelephoneNumber($debtor->office_phone);

        $debtor->mobile_search = $this->prepareTelephoneNumber($debtor->mobile);
        $debtor->private_phone_search = $this->prepareTelephoneNumber($debtor->private_phone);
    }


    /**
     * This method imports the debtors from a given excel file
     *
     * @param $request
     */
    public function importDebtors($request)
    {

        //Todo make file dynamic
        $file = $request['import_file'];
        //todo make type dynamic
        $type = $request->get('debtors_type');

        //Check if the file exists
        if (!file_exists($file)) dd('filefout');

        //Load the file in th excel class
        $this->excel->load($file, function ($reader) use ($type) {
            //Loop trough each sheet
            $reader->each(function ($sheet) use ($type) {
                //Loop trough each row
                $sheet->each(function ($row) use ($type) {

                    //If there is no relatie_code, skip this row
                    if ((!isset($row["relatie_code"]) || $row["relatie_code"] == '') && (!isset($row["relatiecode"]) || $row["relatiecode"] == '')) return;

                    //Prepare the input to a correct format
                    $input = $this->prepareExcelInput($row, $type);

                    //Get the debtor based on the debtors_code
                    $debtor = $this->getDebtorByCode($input['debtors_code']);
                    //Save the debtor to the db
                    $this->saveDebtor($input, $debtor);
                });
            });
        });

        return;
    }


    /**
     * Prepare the excel row to an inpyt array
     *
     * @param $row
     * @param $type
     * @return array
     */
    private function prepareExcelInput($row, $type)
    {
        //Company
        $input['company_name'] = $this->getDataIfExist($row, "bedrijfsnaam");
        $input['company_type'] = $this->getDataIfExist($row, "type_bedrijf");

        //Office
        $input['office_street'] = $this->getDataIfExist($row, "straatnaam_vestigingsadres");
        $input['office_address_2'] = $this->getDataIfExist($row, "adresregel_2_vestigingsadres");
        $input['office_nr'] = $this->getDataIfExist($row, "huisnummer_vestigingsadres");
        $input['office_addition'] = $this->getDataIfExist($row, "toevoeging_vestigingsadres");
        $input['office_postal'] = $this->getDataIfExist($row, "postcode_vestigingsadres");
        $input['office_city'] = $this->getDataIfExist($row, "woonplaats_vestigingsadres");

        //Get the country_id
        if ($country_id = $this->getDataIfExist($row, "land_vestigingsadres")) {
            if ($country = $this->countryService->getCountryByIso2($row["land_vestigingsadres"])) $country_id = $country->id;
        }
        $input['office_country'] = $country_id;

        //Post
        $input['post_street'] = $this->getDataIfExist($row, "straatnaam_postadres");
        $input['post_address_2'] = $this->getDataIfExist($row, "adresregel_2_postadres");
        $input['post_nr'] = $this->getDataIfExist($row, "huisnummer_postadres");
        $input['post_addition'] = $this->getDataIfExist($row, "toevoeging_postadres");
        $input['post_postal'] = $this->getDataIfExist($row, "postcode_postadres");
        $input['post_city'] = $this->getDataIfExist($row, "woonplaats_postadres");

        //Get the country_id
        if ($country_id = $this->getDataIfExist($row, "land_postadres")) {
            if ($country = $this->countryService->getCountryByIso2($row["land_postadres"])) $country_id = $country->id;
        }
        $input['post_country'] = $country_id;


        $input['company_phone'] = $this->getDataIfExist($row, "telefoonnummer_bedrijf");
        $input['office_phone'] = $this->getDataIfExist($row, "telefoonnummer_vestiging");
        $input['fax'] = $this->getDataIfExist($row, "faxnummer");
        $input['mobile'] = $this->getDataIfExist($row, "mobiel");
        $input['email'] = $this->getDataIfExist($row, "algemeen_e_mail_adres");
        $input['remarks'] = $this->getDataIfExist($row, "opmerkingen");
        $input['debtors_code'] = $this->getDataIfExist($row, "relatie_code");
        if (!$input['debtors_code']) $input['debtors_code'] = $this->getDataIfExist($row, "relatiecode");


        //Private debtor
        $input['sex'] = $this->getDataIfExist($row, "geslacht");
        $input['initials'] = $this->getDataIfExist($row, "initialen");
        $input['name_insertion'] = $this->getDataIfExist($row, "tussenvoegsel");
        $input['last_name'] = $this->getDataIfExist($row, "achternaam");
        $input['nick_name'] = $this->getDataIfExist($row, "roepnaam");

        //Private debtor address
        $input['private_phone'] = $this->getDataIfExist($row, "tel_prive");
        $input['private_street'] = $this->getDataIfExist($row, "straatnaam_huisadres");
        $input['private_address_2'] = $this->getDataIfExist($row, "adresregel_2_huisadres");
        $input['private_nr'] = $this->getDataIfExist($row, "huisnummer_huisadres");
        $input['private_addition'] = $this->getDataIfExist($row, "toevoeging_huisadres");
        $input['private_postal'] = $this->getDataIfExist($row, "postcode_huisadres");
        $input['private_city'] = $this->getDataIfExist($row, "woonplaats_huisadres");

        if ($country_id = $this->getDataIfExist($row, "land_huisadres")) {
            if ($country = $this->countryService->getCountryByIso2($row["land_huisadres"])) $country_id = $country->id;
        }

        $input['private_country'] = $country_id;
        $input['debt_status'] = $this->getDataIfExist($row, "alert_opmerkingen", "O");
        $input['debtors_type'] = $type;

        //todo make dynamic

        $input['debt_remarks'] = '';


        return $input;

    }

    /**
     * Get data if the field exist.
     * If it does not exist, return the default.s
     *
     * @param $data
     * @param $field
     * @param null $default
     * @return null
     */
    private function getDataIfExist($data, $field, $default = null)
    {
        if (!isset($data[$field])) return $default;

        if ($data[$field] == null) return $default;

        return $data[$field];

    }


}