<commit_before><?php

/**
 * Manages customer entities
 *
 * @package     Nails
 * @subpackage  module-invoice
 * @category    Model
 * @author      Nails Dev Team
 * @link
 */

namespace Nails\Invoice\Model;

use Nails\Factory;
use Nails\Common\Model\Base;

class Customer extends Base
{
    /**
     * Construct the model
     */
    public function __construct()
    {
        parent::__construct();
        $this->table             = NAILS_DB_PREFIX . 'invoice_customer';
        $this->tablePrefix       = 'c';
        $this->defaultSortColumn = 'first_name';
        $this->destructiveDelete = false;
    }

    // --------------------------------------------------------------------------

    /**
     * Retrieve all customers from the databases
     * @param  int     $iPage           The page number to return
     * @param  int     $iPerPage        The number of results per page
     * @param  array   $aData           Data to pass _to getcount_common()
     * @param  boolean $bIncludeDeleted Whether to include deleted results
     * @return array
     */
    public function getAll($iPage = null, $iPerPage = null, $aData = array(), $bIncludeDeleted = false)
    {
        $aItems = parent::getAll($iPage, $iPerPage, $aData, $bIncludeDeleted);

        if (!empty($aItems)) {

            if (!empty($aData['includeAll']) || !empty($aData['includeInvoices'])) {
                $this->getManyAssociatedItems(
                    $aItems,
                    'invoices',
                    'customer_id',
                    'Invoice',
                    'nailsapp/module-invoice'
                );
            }
        }

        return $aItems;
    }

    // --------------------------------------------------------------------------

    /**
     * Create a new customer
     * @param  array   $aData         The data to create the customer with
     * @param  boolean $bReturnObject Whether to return the complete customer object
     * @return mixed<commit_msg>Adding getCountCommon for admin search/filter<commit_after><?php

/**
 * Manages customer entities
 *
 * @package     Nails
 * @subpackage  module-invoice
 * @category    Model
 * @author      Nails Dev Team
 * @link
 */

namespace Nails\Invoice\Model;

use Nails\Factory;
use Nails\Common\Model\Base;

class Customer extends Base
{
    /**
     * Construct the model
     */
    public function __construct()
    {
        parent::__construct();
        $this->table             = NAILS_DB_PREFIX . 'invoice_customer';
        $this->tablePrefix       = 'c';
        $this->defaultSortColumn = 'first_name';
        $this->destructiveDelete = false;
    }

    // --------------------------------------------------------------------------

    /**
     * Retrieve all customers from the databases
     * @param  int     $iPage           The page number to return
     * @param  int     $iPerPage        The number of results per page
     * @param  array   $aData           Data to pass _to getcount_common()
     * @param  boolean $bIncludeDeleted Whether to include deleted results
     * @return array
     */
    public function getAll($iPage = null, $iPerPage = null, $aData = array(), $bIncludeDeleted = false)
    {
        $aItems = parent::getAll($iPage, $iPerPage, $aData, $bIncludeDeleted);

        if (!empty($aItems)) {

            if (!empty($aData['includeAll']) || !empty($aData['includeInvoices'])) {
                $this->getManyAssociatedItems(
                    $aItems,
                    'invoices',
                    'customer_id',
                    'Invoice',
                    'nailsapp/module-invoice'
                );
            }
        }

        return $aItems;
    }

    // --------------------------------------------------------------------------

    /**
     * This method applies the conditionals which are common across the get_*()
     * methods and the count() method.
     * @param string $data Data passed from the calling method
     * @return void
     */
    protected function getCountCommon($data = array())
    {
        //  If there's a search term, then we better get %LIKING%
        if (!empty($data['keywords'])) {

            if (empty($data['or_like'])) {

                $data['or_like'] = array();
            }

            $keywordAsId = (int) preg_replace('/[^0-9]/', '', $data['keywords']);

            if ($keywordAsId) {

                $data['or_like'][] = array(
                    'column' => $this->tablePrefix . '.id',
                    'value'  => $keywordAsId
                );
            }
            $data['or_like'][] = array(
                'column' => $this->tablePrefix . '.label',
                'value'  => $data['keywords']
            );
        }

        // --------------------------------------------------------------------------

        //  Let the parent method handle sorting, etc
        parent::getCountCommon($data);
    }

    // --------------------------------------------------------------------------

    /**
     * Create a new customer
     * @param  array   $aData         The data to create the customer with
     * @param  boolean $bReturnObject Whether to return the complete customer object
     * @return mixed
{'fraction_non_alphanumeric': 0.19219736087205966, 'fraction_numerical': 0.00038248231019315357, 'mean_word_length': 1.38703788224555, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4589040', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1321, 'n_words': 350}