<?php
App::uses('AppController', 'Controller');
class InvoicesController extends AppController
{
public $key = 'Dawn Consultant Service Invoices';
public $components = array('Paginator', 'Session', 'Flash');
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('admin_add');
}
public function admin_add($encrypted_applicant_id = null)
{
// DCS[YEAR]/[ApplicantNumber]/LASTNUMBER +1
// invoice number e.g. DCS2019/0009/001
if ($this->request->is('post')) {
if (!empty($this->request->data['Invoice'])) {
$this->request->data['ApplicantFilter']['applicant_id'] = $this->request->data['Invoice']['applicant_id'];
// Generate Invoice number
$last_number = '000';
if (isset($this->request->data['Invoice']['last_invoice_number'])) {
$exp = explode('/', $this->request->data['Invoice']['last_invoice_number']);
$last_number = (int)$exp[2];
}
$last_number = (int)$last_number + 1;
$last_number = (string)str_pad($last_number, 3, '0', STR_PAD_LEFT);
$this->request->data['Invoice']['invoice_number'] = 'DCS' . date('Y');
$this->request->data['Invoice']['invoice_number'] .= '/' . str_pad($this->request->data['Invoice']['applicant_id'], 4, '0', STR_PAD_LEFT);
$this->request->data['Invoice']['invoice_number'] .= '/' . $last_number;
$this->request->data['Invoice']['token'] = md5($this->request->data['Invoice']['applicant_id'] . '-' . date("Y-m-d H:i:s"));
$this->Invoice->create();
if ($this->Invoice->save($this->request->data)) {
$this->Flash->success(__('The Invoice has successfully added.'));
$this->redirect('/admin/add_invoice/' . $this->request->data['Invoice']['applicant_id']);
} else {
$this->Flash->error(__('The Invoice could not be saved. Please, try again.'));
}
}
if (!empty($this->request->data['ApplicantFilter']['applicant_id'])) {
//$this->request->data['Invoice']['applicant_id'] = $this->request->data['ApplicantFilter']['applicant_id'];
}
} else {
if ($encrypted_applicant_id != null) {
$this->request->data['ApplicantFilter']['applicant_id'] = $encrypted_applicant_id;
}
}
$query = '
SELECT Applicant.id, Applicant.firstname, Applicant.lastname, Category.name
FROM applicants as Applicant JOIN categories as Category ON Applicant.category_id = Category.id
WHERE Applicant.status_type_id = 1
ORDER BY Applicant.firstname DESC
';
$this->loadModel('Applicant');
$data_applicants = $this->Applicant->query($query);
// pr($data_applicants);
$list_applicants = array();
$list_category_applicants = array();
if (!empty($data_applicants)) {
foreach ($data_applicants as $value) {
$list_category_applicants[$value['Category']['name']][] = $value['Applicant'];
$list_applicants[$value['Applicant']['id']] = $value['Applicant']['firstname'] . ' ' . $value['Applicant']['lastname'] . ' - (' . $value['Category']['name'] . ')';
}
}
// set invoice listing
$invoices = array();
if (isset($this->request->data['ApplicantFilter']['applicant_id'])) {
$invoices = $this->Invoice->find('all', array('recursive' => -1,
'conditions' => array('applicant_id' => $this->request->data['ApplicantFilter']['applicant_id'], 'is_deleted' => false),
'order' => 'created DESC'
));
}
$this->set(compact('list_applicants', 'list_category_applicants', 'invoices'));
}
public function admin_delete($invoice_token = null, $encrypted_applicant_id = null)
{
$this->Invoice->recursive = -1;
if ($invoice_token == null || $encrypted_applicant_id == null) {
$this->redirect('/admin/add_invoice');
}
if ($this->Invoice->find('count', array('conditions' => array('token' => $invoice_token), 'recursive' => -1))) {
$query = "UPDATE invoices SET is_deleted = 1, invoice_number = CONCAT(invoice_number, '_".rand(100, 9999)."') WHERE token = '$invoice_token'";
$this->Invoice->query($query);
$this->Flash->success(__('The Invoice successfully deleted.'));
$this->redirect('/admin/add_invoice/' . $encrypted_applicant_id);
}
}
public function admin_download($invoice_token = null)
{
$this->layout = 'pdf';
if ($invoice_token == null) {
throw new NotFoundException('Invoice could not found');
}
$invoice = $this->Invoice->find('first', array(
'recursive' => 0,
'conditions' => array('Invoice.token' => $invoice_token)
));
if (!empty ($invoice)) {
$this->set('invoice', $invoice);
} else {
throw new NotFoundException('Invoice could not found');
}
}
}