File: /var/www/html/inventory.breadsecret.com/class/Controller/supplierCategory.php
<?php
namespace Controller;
use Responses\Message, Responses\Action, Responses\Data;
use Database\Sql, Database\Listable;
use Pages\Page, Pages\ListPage, Pages\FormPage;
use Routing\Route;
use Utility\WebSystem, Utility\Excel, Utility\Email;
use Controller\formLayout;
class supplierCategory implements Listable {
private $stmStatus = null;
public static function find($id, $fetchMode=\PDO::FETCH_OBJ) {
$sql = Sql::select("supplier_category")->where(['id', '=', $id]);
$stm = $sql->prepare();
$stm->execute();
$obj = $stm->fetch($fetchMode);
if ($obj === false) return null;
return $obj;
}
public static function getStockDetail($request, $fetchMode=\PDO::FETCH_OBJ) {
if (!user::checklogin()) return new Data(['success'=>false, 'message'=>L('login.signInMessage'), 'note'=>'signIn']);
if (!isset($request->get->id) || empty($request->get->id))
return new Data(['success'=>false, 'message'=>L('error.supplierCategoryEmptyID')]);
$supplierCategoryObj = self::find($request->get->id);
if(is_null($supplierCategoryObj)) return new Data(['success'=>false, 'message'=>L('error.supplierCategoryNotFound')]);
$content = "<div class='row'>";
$content .= formLayout::rowInputNew(L('supplierCategory.name'),'name', 'name', 'text', 6, [], ['disabled'], $supplierCategoryObj->name);
//$content .= formLayout::rowDisplayLineNew(L('supplierCategory.name'),$supplierCategoryObj->name);
$sql = Sql::select(['item', 'item'])
->leftJoin(['item_type', 'itemType'], "item.itemTypeID = itemType.id")
->leftJoin(['supplier', 'supplier'], "item.supplierID = supplier.id")
->leftJoin(['unit', 'unit'], "item.unitID = unit.id")
->leftJoin(['item_stock', 'stock'], "stock.itemID = item.id")
->leftJoin(['storage_location_area', 'area'], "stock.storageLocationAreaID = area.id")
->leftJoin(['storage_location', 'location'], "area.storageLocationID = location.id")
->leftJoin(['item_stock_detail', 'stockDetail'], "stockDetail.itemStockID = stock.id")
->where(['item.qty', '>', 0])->where(['stock.qty', '>', 0])->where(['stockDetail.qty', '>', 0])->where(['supplier.supplierCategoryID', '=', $request->get->id]);
$sql->setFieldValue('
stockDetail.id id,
item.name itemName,
itemType.name itemTypeName,
supplier.name supplierName,
area.name areaName,
location.name locationName,
stockDetail.batchNo batchNo,
stockDetail.qty qty,
unit.name unitName,
stockDetail.purchaseUnitCost purchaseUnitCost
');
$stm = $sql->prepare();
$stm->execute();
$obj = $stm->fetchAll($fetchMode);
if ($obj === false) return new Data(['success'=>false, 'message'=>L('error.supplierCategoryNotFound')]);
$data = [];
$totalValue = 0;
foreach($obj as $stockDetail){
$totalValue += $stockDetail->qty*$stockDetail->purchaseUnitCost;
$data[] = [
"item"=>$stockDetail->itemName,
"itemType"=>$stockDetail->itemTypeName,
"supplier"=>$stockDetail->supplierName,
"location"=>$stockDetail->locationName." - ".$stockDetail->areaName,
"batch"=>$stockDetail->batchNo,
"qty"=>$stockDetail->qty,
"unit"=>$stockDetail->unitName,
"cost"=>$stockDetail->purchaseUnitCost,
"value"=>$stockDetail->qty*$stockDetail->purchaseUnitCost
];
}
//$content .= formLayout::rowDisplayLineNew(L('stock.value'),$totalValue);
$content .= formLayout::rowInputNew(L('stock.value'),'', '', 'text', 6, [], ['disabled'], $totalValue);
$content .= "</div>";
$content .= formLayout::rowSeparatorLineNew(12);
$content .= "<div class='table-responsive'><table class='table table-bordered mt-3'>";
$content .= "<thead class='thead-dark'>";
$content .= "<tr>";
$content .= "<th>".L('item.name')."</th>";
$content .= "<th>".L('itemType.name')."</th>";
$content .= "<th>".L('supplier.name')."</th>";
$content .= "<th>".L('stock.storageLocation')."</th>";
$content .= "<th>".L('stock.batchNo')."</th>";
$content .= "<th>".L('stock.qty')."</th>";
$content .= "<th>".L('item.unit')."</th>";
$content .= "<th>".L('stock.purchaseUnitCost')."</th>";
$content .= "<th>".L('stock.value')."</th>";
$content .= "</tr>";
$content .= "</thead>";
foreach($data as $displayData) {
$content .= "<tr>";
$content .= "<td>".$displayData['item']."</td>";
$content .= "<td>".$displayData['itemType']."</td>";
$content .= "<td>".$displayData['supplier']."</td>";
$content .= "<td>".$displayData['location']."</td>";
$content .= "<td>".$displayData['batch']."</td>";
$content .= "<td>".$displayData['qty']."</td>";
$content .= "<td>".$displayData['unit']."</td>";
$content .= "<td>".$displayData['cost']."</td>";
$content .= "<td>".$displayData['value']."</td>";
$content .= "</tr>";
}
$content .= "</table></div>";
return new Data(['success'=>true, 'message'=>$content]);
}
public function extraProcess($listObj) {
if (is_null($this->stmStatus))
$this->stmStatus = Sql::select('status')->where(['id', '=', "?"])->prepare();
$this->stmStatus->execute([$listObj->status]);
$objStatus = $this->stmStatus->fetch();
$listObj->statusName = $objStatus['name'];
return $listObj;
}
public function list($request) {
if (!user::checklogin()) return new Action('redirect', WebSystem::path(Route::getRouteByName('page.login')->path(), false, false));
$obj = null;
return new FormPage('supplierCategory/list', $obj);
}
public function delete($request) {
if (!user::checklogin())
return new Data(['success'=>false, 'message'=>L('login.signInMessage'), 'note'=>'signIn']);
if (!isset($request->get->id) || empty($request->get->id))
return new Data(['success'=>false, 'message'=>L('error.supplierCategoryEmptyID')]);
$sql = Sql::delete('supplier_category')->where(['id', '=', $request->get->id]);
if ($sql->prepare()->execute()) {
return new Data(['success'=>true, 'message'=>L('info.supplierCategoryDeleted')]);
} else {
return new Data(['success'=>false, 'message'=>L('error.supplierCategoryDeleteFailed')]);
}
}
public function supplierCategoryForm($request) {
if (!user::checklogin()) return new Data(['success'=>false, 'message'=>L('login.signInMessage'), 'note'=>'signIn']);
$currentUserObj = unserialize($_SESSION['user']);
$obj = null;
if (isset($request->get->id))
$obj = self::find($request->get->id, \PDO::FETCH_NAMED);
$formName = "form-addSupplierCategory";
$viewMode = isset($request->get->view);
if(!is_null($obj)) {
if(isset($request->get->view)){
$formName = "form-viewSupplierCategory";
}
else{
$formName = "form-editSupplierCategory";
}
}
$content = "<form id='".$formName."' class='' autocomplete='off'>";
$content .= "<div class='row'><p class='col-md-12 col-lg-12 text-primary' id='notice'>".L('info.supplierCategoryAddHelperMessage')."</p></div>";
$content .= formLayout::rowInputNew(L('supplierCategory.name'),'name', 'name', 'text', 12, [], [$viewMode ? 'disabled' : 'required'], is_null($obj)?'':$obj['name']);
if(!is_null($obj)) {
$option = [];
$stm = Sql::select('status')->prepare();
$stm->execute();
foreach ($stm as $opt) {
$option[$opt['id']] = L($opt['name']);
}
$content .= formLayout::rowSelectNew(L('Status'), 'status', 'status', $option, 12, [], [$viewMode ? 'disabled' : 'required'], is_null($obj)?'':$obj['status']);
}
$content .= "</form>";
return new Data(['success'=>true, 'message'=>$content]);
}
public function add($request) {
if (!user::checklogin())
return new Data(['success'=>false, 'message'=>L('login.signInMessage'), 'note'=>'signIn']);
$currentUserObj = unserialize($_SESSION['user']);
// form check
if (!isset($request->post->name) || empty($request->post->name))
return new Data(['success'=>false, 'message'=>L('error.supplierCategoryEmptyName'), 'field'=>'name']);
// insert database
$sql = Sql::insert('supplier_category')->setFieldValue([
'name' => "?"
]);
if ($sql->prepare()->execute([
strip_tags($request->post->name),
])) {
$id = db()->lastInsertId();
return new Data(['success'=>true, 'message'=>L('info.saved'), 'id'=>$id, 'name'=>$request->post->name]);
} else {
return new Data(['success'=>false, 'message'=>L('error.unableInsert'), 'field'=>'notice']);
}
}
public function edit($request) {
if (!user::checklogin())
return new Data(['success'=>false, 'message'=>L('login.signInMessage'), 'note'=>'signIn']);
$currentUserObj = unserialize($_SESSION['user']);
if (!isset($request->get->id) || empty($request->get->id))
return new Data(['success'=>false, 'message'=>L('error.supplierCategoryEmptyID'), 'field'=>'notice']);
$rntpcObj = self::find($request->get->id);
if(is_null($rntpcObj))
return new Data(['success'=>false, 'message'=>L('error.supplierCategoryNotFound'), 'field'=>'notice']);
// form check
if (!isset($request->post->name) || empty($request->post->name))
return new Data(['success'=>false, 'message'=>L('error.supplierCategoryEmptyName'), 'field'=>'name']);
$editFields = [];
$editValues = [];
if (isset($request->post->name) && !empty($request->post->name)) {
$editFields['name'] = "?";
$editValues[] = $request->post->name;
}
if (isset($request->post->status) && !empty($request->post->status)) {
$editFields['status'] = "?";
$editValues[] = $request->post->status;
}
/*
if (count($editFields)) {
$editFields['modifyDate'] = "NOW()";
$editFields['modifyBy'] = $currentUserObj->id;
}
*/
if (count($editFields) == 0) return new Data(['success'=>false, 'message'=>L('error.nothingEdit'), 'field'=>'notice']);
$sql = Sql::update('supplier_category')->setFieldValue($editFields)->where(['id', '=', $request->get->id]);
if ($sql->prepare()->execute($editValues)) {
return new Data(['success'=>true, 'message'=>L('info.updated')]);
} else {
return new Data(['success'=>false, 'message'=>L('error.unableUpdate'), 'field'=>'notice']);
}
}
public static function genTableHeader() {
$htmlContent = "";
$htmlContent .= "<thead class='thead-dark'>";
$htmlContent .= "<tr>";
$htmlContent .= "<th>".L('ID')."</th>";
$htmlContent .= "<th>".L('supplierCategory.name')."</th>";
$htmlContent .= "<th>".L('Status')."</th>";
$htmlContent .= "<th>".L('Actions')."</th>";
$htmlContent .= "</tr>";
$htmlContent .= "</thead>";
return $htmlContent;
}
public static function genTableFooter() {
$htmlContent = "";
$htmlContent .= "<tfoot>";
$htmlContent .= "<tr>";
$htmlContent .= "<th>".L('ID')."</th>";
$htmlContent .= "<th>".L('supplierCategory.name')."</th>";
$htmlContent .= "<th>".L('Status')."</th>";
$htmlContent .= "<th></th>";
$htmlContent .= "</tr>";
$htmlContent .= "</tfoot>";
return $htmlContent;
}
public static function genTableContentData() {
$sql = Sql::select(['supplier_category', 'supplierCategory'])->leftJoin(['status', 'status'], "supplierCategory.status = status.id");
$sql->setFieldValue('
supplierCategory.id id,
supplierCategory.name name,
status.name statusName
');
$stm = $sql->prepare();
$stm->execute();
return $stm;
}
public static function genTableBodyRow($listObj) {
$htmlContent = "";
$htmlContent .= "<tr>";
$htmlContent .= "<td>".$listObj['id']."</td>";
$htmlContent .= "<td>".$listObj['name']."</td>";
$htmlContent .= "<td>".L($listObj['statusName'])."</td>";
$htmlContent .= "<td>";
$htmlContent .= "<div class='btn-group' role='group' aria-label=''>";
$htmlContent .= "<button class='btn btn-sm btn-dark btnView' type='button' data-bs-toggle='tooltip' data-bs-placement='top' title='".L('menu.inventoryMain')."' data-id='".$listObj['id']."'><i class='fas fa-sm fa-th'></i></button>";
$htmlContent .= "<button class='btn btn-sm btn-success btnEdit' type='button' data-bs-toggle='tooltip' data-bs-placement='top' title='".L('Edit')."' data-id='".$listObj['id']."'><i class='fas fa-sm fa-edit'></i></button>";
$htmlContent .= "<button class='btn btn-sm btn-danger btnDel' type='button' data-bs-toggle='tooltip' data-bs-placement='top' title='".L('Delete')."' data-id='".$listObj['id']."'><i class='fas fa-sm fa-trash-alt'></i></button>";
$htmlContent .= "</div>";
$htmlContent .= "</td>";
$htmlContent .= "</tr>";
return $htmlContent;
}
}