You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

381 lines
16 KiB

<?php
namespace App\Controllers;
use App\Models\Company;
use App\Models\Orders;
use App\Models\Products;
use App\Models\Avance;
class AvanceController extends AdminController
{
private $pageTitle = 'Avances';
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->verifyRole('viewAvance');
$data['page_title'] = $this->pageTitle;
$Products = new Products();
$session = session();
$users = $session->get('user');
$store_id = $users['store_id'];
$data['products'] = $Products->getProductDataStore($store_id);
return $this->render_template('avances/avance', $data);
}
public function fetchAvanceData()
{
helper(['url', 'form']);
$Avance = new Avance();
$product = new Products();
$result = ['data' => []];
$data = $Avance->getAllAvanceData();
$session = session();
$users = $session->get('user');
$isAdmin = in_array($users['group_name'], ['Conseil', 'Direction']);
$isCommerciale = in_array($users['group_name'], ['COMMERCIALE']);
$isCaissier = in_array($users['group_name'], ['Caissier']);
foreach ($data as $key => $value) {
$isOwner = $users['id'] === $value['user_id'];
$date_time = date('d-m-Y h:i a', strtotime($value['avance_date']));
// Boutons d’action
$buttons = '';
if (in_array('updateAvance', $this->permission) && ($isAdmin || $isOwner)) {
$buttons .= '<button type="button" class="btn btn-default" onclick="editFunc('. $value['avance_id'] .')">'
. '<i class="fa fa-pencil"></i></button>';
}
if (in_array('deleteAvance', $this->permission) && ($isAdmin || $isOwner)) {
$buttons .= '<button type="button" class="btn btn-danger" onclick="removeFunc(' . $value['avance_id'] . ',' . $value['product_id'] . ')"><i class="fa fa-trash"></i></button>';
}
if (in_array('viewAvance', $this->permission) && !$isAdmin) {
$buttons .= ' <a href="#" data-order-id="'.$value['id'].'" class="btn btn-default btn-view" title="Voir"><i class="fa fa-eye"></i></a>';
}
if ($isAdmin) {
$row = [
$value['customer_name'],
$value['customer_phone'],
$value['customer_address'],
$product->getProductNameById($value['product_id']),
number_format((int)$value['gross_amount'], 0, ',', ' '),
number_format((int)$value['avance_amount'], 0, ',', ' '),
number_format((int)$value['amount_due'], 0, ',', ' '),
$date_time,
$buttons,
];
// dd($row);die;
$result['data'][] = $row;
}
if ($isCommerciale || $isCaissier) {
$row = [
$value['avance_id'],
number_format((int)$value['avance_amount'], 0, ',', ' '),
number_format((int)$value['amount_due'], 0, ',', ' '),
$date_time,
$buttons,
];
$result['data'][] = $row;
}
}
return $this->response->setJSON($result);
}
public function createAvance()
{
// $this->verifyRole('createAvance');
$data['page_title'] = $this->pageTitle;
$Avance = new Avance();
$Products = new Products();
$Notification = New NotificationController();
if ($this->request->getMethod() === 'post') {
$session = session();
$users = $session->get('user');
$data = [
'customer_name' => $this->request->getPost('customer_name_avance'),
'customer_address' => $this->request->getPost('customer_address_avance'),
'customer_phone' => $this->request->getPost('customer_phone_avance'),
'customer_cin' => $this->request->getPost('customer_cin_avance'),
'avance_date' => date('Y-m-d'),
'user_id' => $users['id'],
'store_id' => $users['store_id'],
'product_id' => $this->request->getPost('id_product'),
'gross_amount' => (float)$this->request->getPost('gross_amount'),
'avance_amount' => (float)$this->request->getPost('avance_amount'),
'amount_due' => (float)$this->request->getPost('amount_due'),
'is_order' => (float)0,
'active' => 1,
];
if($avance_id = $Avance->createAvance($data)){
$product = new Products();
$product->update((int)$this->request->getPost('id_product'), ['product_sold' => 1]);
$Notification->createNotification('Une avance a été créé', "Conseil",(int)$users['store_id'], 'avances');
return $this->response->setJSON([
'success' => true,
'messages' => 'Avance créé avec succès !'
]);
}
else{
return $this->response->setJSON([
'success' => false,
'messages' => 'Une erreur est survenue lors de la création d\une avance !'
]);
}
}
}
public function updateAvance(int $id)
{
$this->verifyRole('updateAvance');
$data['page_title'] = $this->pageTitle;
$Products = new Products();
$Avance = new Avance();
$session = session();
$users = $session->get('user');
if ($this->request->getMethod() === 'post') {
$data = [
'customer_name' => $this->request->getPost('customer_name_avance_edit'),
'customer_address'=> $this->request->getPost('customer_address_avance_edit'),
'customer_phone' => $this->request->getPost('customer_phone_avance_edit'),
'customer_cin' => $this->request->getPost('customer_cin_avance_edit'),
'gross_amout' => $this->request->getPost('gros_amount_edit'),
'avance_amount' => (int)$this->request->getPost('avance_amount_edit'),
'amount_due' => (int)$this->request->getPost('amount_due_edit'),
'product_id' => $this->request->getPost('id_product_edit'),
];
$bill_no = 'BILPR-' . strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 4));
$Company = new Company();
$company = $Company->getCompanyData(1);
$company['vat_charge_value'] > 0;
$service_charge_rate = $company['service_charge_value'];
$vat_charge_rate = $company['vat_charge_value'];
$gross_amount = $this->request->getPost('gross_amount_edit');
$vat_charge = ($gross_amount / 100) * $vat_charge_rate;
$amount_due = (int)$this->request->getPost('amount_due_edit');
$product_id = (array)$this->request->getPost('id_product_edit');
if ($amount_due <= 0) {
$Orders = new Orders();
$data = [
'bill_no' => $bill_no,
'customer_name' => $this->request->getPost('customer_name_avance_edit'),
'customer_address'=> $this->request->getPost('customer_address_avance_edit'),
'customer_phone' => $this->request->getPost('customer_phone_avance_edit'),
'customer_cin' => $this->request->getPost('customer_cin_avance_edit'),
'gross_amout' => $gross_amount,
'net_amount' => $gross_amount,
'date_time' => date('Y-m-d H:i:s'),
'service_charge_rate' => $service_charge_rate,
'vat_charge_rate' => $vat_charge_rate,
'vat_charge' => $vat_charge,
'discount' => (int) 0,
'paid_status' => 1,
'user_id' => $users['id'],
'store_id' => $users['store_id'],
'amount_value' => $gross_amount,
'rate_value' => $gross_amount,
];
$data1 = ['is_order' => 1];
if($Orders->create($data,$product_id)){
$Avance->updateAvance($id,$data1);
$Notification = New NotificationController();
$Notification->createNotification('Une commande a été créé', "Conseil",(int)$users['store_id'], 'orders');
return $this->response->setJSON([
'success' => true,
'messages' => 'success. Avance convertie en commande avec succès.'
]);
}
else{
return $this->response->setJSON([
'success' => false,
'messages' => 'Erreur lors de la convertion de l\'avance'
]);
}
}
else{
if ($Avance->updateAvance($id, $data)) {
return $this->response->setJSON([
'success' => true,
'messages' => 'success', 'Avance mise à jour avec succès.'
]);
} else {
return $this->response->setJSON([
'success' => true,
'messages' => 'Errors', 'Une erreur est survenue lors de la mise à jour.'
]);
}
}
}
}
public function removeAvance()
{
$this->verifyRole('deleteAvance');
$avance_id = $this->request->getPost('avance_id');
$product_id = $this->request->getPost('product_id');
$response = [];
$Avance = new Avance();
if ($Avance->removeAvance($avance_id)) {
$product = new Products();
$product->update($product_id, ['product_sold' => 0]);
$response['success'] = true;
$response['messages'] = "Avance supprimée avec succès. Ce produit peut désormais être réservé à nouveau.";
} else {
$response['success'] = false;
$response['messages'] = "une erreur est survenue lors de la suppression d'une avance";
}
return $this->response->setJSON($response);
}
public function fetchSingleAvance($avance_id)
{
$this->verifyRole('updateAvance');
try {
$avanceModel = new Avance();
$data = $avanceModel->fetchSingleAvance($avance_id);
return $this->response->setJSON($data);
}
catch (\Throwable $th) {
log_message('error', "Erreur lors de la récupération d'une avance: " . $th->getMessage());
return $this->response
->setStatusCode(500)
->setJSON(['error' => 'Une erreur interne est survenue. Lors de la création d\'une avance']);
}
}
public function fetchAvanceBecameOrder()
{
helper(['url', 'form']);
$Avance = new Avance();
$product = new Products();
$result = ['data' => []];
$data = $Avance->getAllAvanceData1();
$session = session();
$users = $session->get('user');
$isAdmin = in_array($users['group_name'], ['Conseil', 'Direction']);
$isCommerciale = in_array($users['group_name'], ['COMMERCIALE']);
$isCaissier = in_array($users['group_name'], ['Caissier']);
foreach ($data as $key => $value) {
$isOwner = $users['id'] === $value['user_id'];
$date_time = date('d-m-Y h:i a', strtotime($value['avance_date']));
// Boutons d’action
$buttons = '';
if (in_array('updateAvance', $this->permission) && ($isAdmin || $isOwner)) {
$buttons .= '<button type="button" class="btn btn-default" onclick="editFunc('. $value['avance_id'] .')">'
. '<i class="fa fa-pencil"></i></button>';
}
if (in_array('deleteAvance', $this->permission) && ($isAdmin || $isOwner)) {
$buttons .= '<button type="button" class="btn btn-danger" onclick="removeFunc(' . $value['avance_id'] . ',' . $value['product_id'] . ')"><i class="fa fa-trash"></i></button>';
}
if (in_array('viewAvance', $this->permission) && !$isAdmin) {
$buttons .= ' <a href="#" data-order-id="'.$value['id'].'" class="btn btn-default btn-view" title="Voir"><i class="fa fa-eye"></i></a>';
}
if ($isAdmin) {
$row = [
$value['customer_name'],
$value['customer_phone'],
$value['customer_address'],
$product->getProductNameById($value['product_id']),
number_format((int)$value['gross_amount'], 0, ',', ' '),
number_format((int)$value['avance_amount'], 0, ',', ' '),
number_format((int)$value['amount_due'], 0, ',', ' '),
$date_time,
$buttons,
];
// dd($row);die;
$result['data'][] = $row;
}
if ($isCommerciale || $isCaissier) {
$row = [
$value['avance_id'],
$product->getProductNameById($value['product_id']),
number_format((int)$value['avance_amount'], 0, ',', ' '),
number_format((int)$value['amount_due'], 0, ',', ' '),
$date_time,
$buttons,
];
$result['data'][] = $row;
}
}
return $this->response->setJSON($result);
}
public function fetcheExpiredAvance()
{
helper(['url', 'form']);
$Avance = new Avance();
$product = new Products();
$result = ['data' => []];
$data = $Avance->getAllAvanceData2();
$session = session();
$users = $session->get('user');
$isAdmin = in_array($users['group_name'], ['Conseil', 'Direction']);
$isCommerciale = in_array($users['group_name'], ['COMMERCIALE']);
$isCaissier = in_array($users['group_name'], ['Caissier']);
foreach ($data as $key => $value) {
$isOwner = $users['id'] === $value['user_id'];
$date_time = date('d-m-Y h:i a', strtotime($value['avance_date']));
// Boutons d’action
$buttons = '';
if (in_array('updateAvance', $this->permission) && ($isAdmin || $isOwner)) {
$buttons .= '<button type="button" class="btn btn-default" onclick="editFunc('. $value['avance_id'] .')">'
. '<i class="fa fa-pencil"></i></button>';
}
if (in_array('deleteAvance', $this->permission) && ($isAdmin || $isOwner)) {
$buttons .= '<button type="button" class="btn btn-danger" onclick="removeFunc(' . $value['avance_id'] . ',' . $value['product_id'] . ')"><i class="fa fa-trash"></i></button>';
}
if (in_array('viewAvance', $this->permission) && !$isAdmin) {
$buttons .= ' <a href="#" data-order-id="'.$value['id'].'" class="btn btn-default btn-view" title="Voir"><i class="fa fa-eye"></i></a>';
}
if ($isAdmin) {
$row = [
$value['customer_name'],
$value['customer_phone'],
$value['customer_address'],
$product->getProductNameById($value['product_id']),
number_format((int)$value['gross_amount'], 0, ',', ' '),
number_format((int)$value['avance_amount'], 0, ',', ' '),
number_format((int)$value['amount_due'], 0, ',', ' '),
$date_time,
$buttons,
];
// dd($row);die;
$result['data'][] = $row;
}
if ($isCommerciale || $isCaissier) {
$row = [
$value['avance_id'],
$product->getProductNameById($value['product_id']),
number_format((int)$value['avance_amount'], 0, ',', ' '),
number_format((int)$value['amount_due'], 0, ',', ' '),
$date_time,
$buttons,
];
$result['data'][] = $row;
}
}
return $this->response->setJSON($result);
}
}