178 lines
6.9 KiB
PHP
178 lines
6.9 KiB
PHP
|
<?php
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace CakeAccounting\Controller;
|
||
|
|
||
|
use Cake\Event\EventInterface;
|
||
|
use Cake\Log\Log;
|
||
|
use CakeAccounting\Controller\AppController;
|
||
|
use CakeAccounting\Service\AccountingService;
|
||
|
|
||
|
/**
|
||
|
* DeAccounts Controller
|
||
|
*
|
||
|
* @property \CakeAccounting\Model\Table\DeAccountsTable $DeAccounts
|
||
|
*/
|
||
|
class DeAccountsController extends AppController
|
||
|
{
|
||
|
/**
|
||
|
* @param EventInterface $event
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function beforeRender(EventInterface $event)
|
||
|
{
|
||
|
parent::beforeRender($event);
|
||
|
$this->viewBuilder()->addHelper('CakeAccounting.Accounting');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Index method
|
||
|
*
|
||
|
* @return \Cake\Http\Response|null|void Renders view
|
||
|
*/
|
||
|
public function index(AccountingService $accounting)
|
||
|
{
|
||
|
$deAccounts = $this->DeAccounts->find('threaded')->contain(['DeAccountTypes'])->toArray();
|
||
|
$totals = [];
|
||
|
foreach ($deAccounts as $deAccount) {
|
||
|
// Log::debug('account in de accounts controller index for loop');
|
||
|
// Log::debug('$deAccount->account_number');
|
||
|
// Log::debug("$deAccount->account_number");
|
||
|
|
||
|
$totals[$deAccount->account_number] = $accounting->getAccountBalance($deAccount);
|
||
|
}
|
||
|
|
||
|
$this->set(compact('deAccounts', 'totals'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* View method
|
||
|
*
|
||
|
* @param string|null $accountNumber De Account account number.
|
||
|
* @return \Cake\Http\Response|null|void Renders view
|
||
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
|
||
|
*/
|
||
|
public function view(AccountingService $accounting, $accountNumber = null, )
|
||
|
{
|
||
|
$deAccount = $this->DeAccounts->find()
|
||
|
->contain(['DeAccountTypes', 'DeAccountStatements'])
|
||
|
->where(['DeAccounts.account_number' => $accountNumber])
|
||
|
->firstOrFail();
|
||
|
$deAccount->children = $this->DeAccounts->find('children', for: $deAccount->id)->find('threaded')->toArray();
|
||
|
$totals[$deAccount->account_number] = $accounting->getAccountBalance($deAccount);
|
||
|
|
||
|
$this->set(compact('deAccount', 'totals'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Add method
|
||
|
*
|
||
|
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
|
||
|
*/
|
||
|
public function add()
|
||
|
{
|
||
|
$deAccount = $this->DeAccounts->newEmptyEntity();
|
||
|
if ($this->request->is('post')) {
|
||
|
$postData = $this->request->getData();
|
||
|
if ($this->request->getData('parent_id')) {
|
||
|
$parent = $this->DeAccounts->find()->where(['DeAccounts.account_number' => $this->request->getData('parent_id')])->first();
|
||
|
if (isset($parent)) {
|
||
|
$postData['parent_id'] = $parent->id;
|
||
|
$postData['account_type_code'] = $parent->account_type_code;
|
||
|
}
|
||
|
}
|
||
|
$deAccount = $this->DeAccounts->patchEntity($deAccount, $postData);
|
||
|
if ($this->DeAccounts->save($deAccount)) {
|
||
|
$this->Flash->success(__('The de account has been saved.'));
|
||
|
|
||
|
return $this->redirect(['action' => 'index']);
|
||
|
}
|
||
|
Log::debug(print_r('$deAccount->getErrors() adding', true));
|
||
|
Log::debug(print_r($deAccount->getErrors(), true));
|
||
|
$this->Flash->error(__('The de account could not be saved. Please, try again.'));
|
||
|
}
|
||
|
$deAccountTypes = $this->DeAccounts->DeAccountTypes->find('list', limit: 200)->all();
|
||
|
$accounts = $this->DeAccounts->find('accountTreeList')->toArray();
|
||
|
$this->set(compact('deAccount', 'deAccountTypes', 'accounts'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Edit method
|
||
|
*
|
||
|
* @param string|null $accountNumber De Account account number.
|
||
|
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
|
||
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
|
||
|
*/
|
||
|
public function edit($accountNumber = null)
|
||
|
{
|
||
|
$deAccount = $this->DeAccounts->find()
|
||
|
->where(['DeAccounts.account_number' => $accountNumber])
|
||
|
->firstOrFail();
|
||
|
$children = $this->DeAccounts->find()->where(['parent_id' => $deAccount->id])->count();
|
||
|
$journalItems = $this->DeAccounts->CreditDeJournalItems->find()
|
||
|
->where([
|
||
|
'OR' => [
|
||
|
'account_number_credit' => $accountNumber,
|
||
|
'account_number_debit' => $accountNumber,
|
||
|
],
|
||
|
])
|
||
|
->count();
|
||
|
if ($children) {
|
||
|
$this->Flash->error('Account cannot be edited as there are are sub-accounts underneath this account.');
|
||
|
}
|
||
|
if ($journalItems) {
|
||
|
$this->Flash->error('Account cannot be edited as journal entries already exist on this account.');
|
||
|
}
|
||
|
if ($children || $journalItems) {
|
||
|
return $this->redirect(['action' => 'view', $accountNumber]);
|
||
|
}
|
||
|
if ($this->request->is(['patch', 'post', 'put'])) {
|
||
|
$postData = $this->request->getData();
|
||
|
if ($this->request->getData('parent_id')) {
|
||
|
$parent = $this->DeAccounts->find()->where(['DeAccounts.account_number' => $this->request->getData('parent_id')])->first();
|
||
|
if (isset($parent)) {
|
||
|
$postData['parent_id'] = $parent->id;
|
||
|
$postData['account_type_code'] = $parent->account_type_code;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$deAccount = $this->DeAccounts->patchEntity($deAccount, $postData);
|
||
|
if ($this->DeAccounts->save($deAccount)) {
|
||
|
$this->Flash->success(__('The de account has been saved.'));
|
||
|
|
||
|
return $this->redirect(['action' => 'index']);
|
||
|
}
|
||
|
Log::debug(print_r('$deAccount->getErrors() editing', true));
|
||
|
Log::debug(print_r($deAccount->getErrors(), true));
|
||
|
|
||
|
$this->Flash->error(__('The de account could not be saved. Please, try again.'));
|
||
|
}
|
||
|
$deAccountTypes = $this->DeAccounts->DeAccountTypes->find('list', limit: 200)->all();
|
||
|
$parentDeAccounts = $this->DeAccounts->ParentDeAccounts->find('list', limit: 200)->all();
|
||
|
$this->set(compact('deAccount', 'deAccountTypes', 'parentDeAccounts'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Delete method
|
||
|
*
|
||
|
* @param string|null $accountNumber De Account id.
|
||
|
* @return \Cake\Http\Response|null Redirects to index.
|
||
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
|
||
|
*/
|
||
|
public function delete($accountNumber = null)
|
||
|
{
|
||
|
$this->request->allowMethod(['post', 'delete']);
|
||
|
$deAccount = $this->DeAccounts->find()
|
||
|
->where(['DeAccounts.account_number' => $accountNumber])
|
||
|
->firstOrFail();
|
||
|
if ($this->DeAccounts->delete($deAccount)) {
|
||
|
$this->Flash->success(__('The de account has been deleted.'));
|
||
|
} else {
|
||
|
$this->Flash->error(__('The de account could not be deleted. Please, try again.'));
|
||
|
}
|
||
|
|
||
|
return $this->redirect(['action' => 'index']);
|
||
|
}
|
||
|
}
|