Files
opensourcepos/app/Models/Module.php
Ollama 62236aec30 refactor: Extract duplicated code into reusable components
- Created app/Traits/Controller/Shared.php with helper methods for supplier info, sale mode labels, company info, and tax code data
- Created app/Traits/Models/Reports/ReportDateFilter.php for date filtering logic across reports
- Created app/Traits/Models/Reports/SaleTypeFilter.php for sale type filtering pattern
- Created app/Traits/Database/SalesTaxMigration.php for migration tax handling
- Refactored Sales.php to use Shared trait for mode labels and company info
- Refactored Taxes.php to use Shared trait for tax code initialization
- Refactored Receivings.php to use Shared trait for supplier info building
- Refactored Summary_report.php, Summary_payments.php, Summary_sales_taxes.php, Summary_expenses_categories.php to use ReportDateFilter and SaleTypeFilter traits
- Refactored Detailed_sales.php to use SaleTypeFilter trait
- Refactored both tax migrations to use SalesTaxMigration trait
- Removed 39 TODO: Duplicated code comments across 19 files

Closes #4490
2026-04-15 12:49:31 +00:00

162 lines
4.8 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Database\ResultInterface;
use CodeIgniter\Model;
/**
* Module class
*/
class Module extends Model
{
protected $table = 'modules';
protected $primaryKey = 'module_id';
protected $useAutoIncrement = false;
protected $useSoftDeletes = false;
protected $allowedFields = [
'name_lang_key',
'desc_lang_key',
'sort'
];
/**
* @param string $module_id
* @return string
*/
public function get_module_name(string $module_id): string
{
$builder = $this->db->table('modules');
$query = $builder->getWhere(['module_id' => $module_id], 1);
if ($query->getNumRows() == 1) { // TODO: ===
$row = $query->getRow();
return lang($row->name_lang_key);
}
return lang('Errors.unknown');
}
/**
* @param string $module_id
* @return string
*/
public function get_module_desc(string $module_id): string // TODO: This method doesn't seem to be called in the code. Is it needed? Also, probably should change the name to get_module_description()
{
$builder = $this->db->table('modules');
$query = $builder->getWhere(['module_id' => $module_id], 1);
if ($query->getNumRows() == 1) { // TODO: ===
$row = $query->getRow();
return lang($row->desc_lang_key);
}
return lang('Errors.unknown');
}
/**
* @return ResultInterface
*/
public function get_all_permissions(): ResultInterface
{
$builder = $this->db->table('permissions');
return $builder->get();
}
/**
* @return ResultInterface
*/
public function get_all_subpermissions(): ResultInterface
{
$builder = $this->db->table('permissions');
$builder->join('modules AS modules', 'modules.module_id = permissions.module_id'); // TODO: can the table parameter just be modules instead of modules AS modules?
// Can't quote the parameters correctly when using different operators
$builder->where('modules.module_id != ', 'permission_id', false);
return $builder->get();
}
/**
* @return ResultInterface
*/
public function get_all_modules(): ResultInterface
{
$builder = $this->db->table('modules');
$builder->orderBy('sort', 'asc');
return $builder->get();
}
/**
* @param int $person_id
* @return ResultInterface
*/
public function get_allowed_home_modules(int $person_id): ResultInterface
{
$menus = ['home', 'both'];
$builder = $this->db->table('modules'); // TODO: this is duplicated with the code below... probably refactor a method and just pass through whether home/office modules are needed.
$builder->join('permissions', 'permissions.permission_id = modules.module_id');
$builder->join('grants', 'permissions.permission_id = grants.permission_id');
$builder->where('person_id', $person_id);
$builder->whereIn('menu_group', $menus);
$builder->where('sort !=', 0);
$builder->orderBy('sort', 'asc');
return $builder->get();
}
/**
* @param int $person_id
* @return ResultInterface
*/
public function get_allowed_office_modules(int $person_id): ResultInterface
{
$menus = ['office', 'both'];
$builder = $this->db->table('modules');
$builder->join('permissions', 'permissions.permission_id = modules.module_id');
$builder->join('grants', 'permissions.permission_id = grants.permission_id');
$builder->where('person_id', $person_id);
$builder->whereIn('menu_group', $menus);
$builder->where('sort !=', 0);
$builder->orderBy('sort', 'asc');
return $builder->get();
}
/**
* This method is used to set the show the office navigation icon on the home page
* which happens when the sort value is greater than zero
*/
public function set_show_office_group(bool $show_office_group): void // TODO: Should we return the value of update() as a bool for consistency?
{
if ($show_office_group) { // TODO: This should be replaced with ternary notation
$sort = 999;
} else {
$sort = 0;
}
$modules_data = ['sort' => $sort];
$builder = $this->db->table('modules');
$builder->where('module_id', 'office');
$builder->update($modules_data);
}
/**
* This method is used to show the office navigation icon on the home page
* which happens when the sort value is greater than zero
*/
public function get_show_office_group(): int
{
$builder = $this->db->table('modules');
$builder->select('sort');
$builder->where('module_id', 'office');
return $builder->get()->getRow()->sort;
}
}