File: /var/www/html/inventory.breadsecret.com/class/Controller/storageLocationArea.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\documentHelper, Controller\formLayout;
class storageLocationArea implements Listable {
private $stmStatus = null;
private $stmsStorageLocation = null;
public static function find($id, $fetchMode=\PDO::FETCH_OBJ) {
$sql = Sql::select("storage_location_area")->where(['id', '=', $id]);
$stm = $sql->prepare();
$stm->execute();
$obj = $stm->fetch($fetchMode);
if ($obj === false) return null;
return $obj;
}
public static function findAll($fetchMode=\PDO::FETCH_OBJ) {
$sql = Sql::select("storage_location_area")->where(['status', '=', "1"]);
$stm = $sql->prepare();
$stm->execute();
return $stm;
}
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.storageLocationAreaEmptyID')]);
$storageLocationAreaObj = self::find($request->get->id);
if(is_null($storageLocationAreaObj)) return new Data(['success'=>false, 'message'=>L('error.storageLocationAreaNotFound')]);
$content = "<div class='row'>";
$content .= formLayout::rowInputNew(L('storageLocation.name'),'name', 'name', 'text', 4, [], ['disabled'], storageLocation::find($storageLocationAreaObj->storageLocationID)->name);
$content .= formLayout::rowInputNew(L('storageLocationArea.name'),'name', 'name', 'text', 4, [], ['disabled'], $storageLocationAreaObj->name);
//$content .= formLayout::rowDisplayLineNew(L('storageLocation.name'),storageLocation::find($storageLocationAreaObj->storageLocationID)->name);
//$content .= formLayout::rowDisplayLineNew(L('storageLocationArea.name'),$storageLocationAreaObj->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(['stock.storageLocationAreaID', '=', $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.storageLocationNotFound')]);
$data = [];
$totalValue = 0;
foreach($obj as $stockDetail){
$totalValue += $stockDetail->qty*$stockDetail->purchaseUnitCost;
$data[] = [
"item"=>$stockDetail->itemName,
"itemType"=>$stockDetail->itemTypeName,
"supplier"=>$stockDetail->supplierName,
"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', 4, [], ['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.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['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->stmsStorageLocation))
$this->stmsStorageLocation = Sql::select('storage_location')->where(['id', '=', "?"])->prepare();
$this->stmsStorageLocation->execute([$listObj->categoryID]);
$objStorageLocation = $this->stmsStorageLocation->fetch();
$listObj->storageLocation = $objStorageLocation['name'];
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('storageLocationArea/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.storageLocationAreaEmptyID')]);
$sql = Sql::delete('storage_location_area')->where(['id', '=', $request->get->id]);
if ($sql->prepare()->execute()) {
return new Data(['success'=>true, 'message'=>L('info.storageLocationAreaDeleted')]);
} else {
return new Data(['success'=>false, 'message'=>L('error.storageLocationAreaDeleteFailed')]);
}
}
public function storageLocationAreaForm($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-addStorageLocationArea";
$viewMode = isset($request->get->view);
if(!is_null($obj)) {
if(isset($request->get->view)){
$formName = "form-viewStorageLocationArea";
}
else{
$formName = "form-editStorageLocationArea";
}
}
$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.storageLocationAreaAddHelperMessage')."</p></div>";
$option = [""=>"", "Add"=>"[".L('Add')."]"];
$stm = Sql::select('storage_location')->where(['status', '=', 1])->prepare();
$stm->execute();
foreach ($stm as $opt) {
$option[$opt['id']] = $opt['name'];
}
$content .= formLayout::rowSelectNew(L('storageLocationArea.storageLocation'), 'storageLocationID', 'storageLocationID', $option, 12, [], [$viewMode ? 'disabled' : 'required'], is_null($obj)?'':$obj['storageLocationID']);
$content .= formLayout::rowInputNew(L('storageLocationArea.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->storageLocationID) || empty($request->post->storageLocationID))
return new Data(['success'=>false, 'message'=>L('error.storageLocationAreaEmptyLocation'), 'field'=>'storageLocationID']);
if (!isset($request->post->name) || empty($request->post->name))
return new Data(['success'=>false, 'message'=>L('error.storageLocationAreaEmptyName'), 'field'=>'name']);
// insert database
$sql = Sql::insert('storage_location_area')->setFieldValue([
'storageLocationID' => "?",
'name' => "?"
]);
if ($sql->prepare()->execute([
strip_tags($request->post->storageLocationID),
strip_tags($request->post->name)
])) {
$id = db()->lastInsertId();
return new Data(['success'=>true, 'message'=>L('info.saved')]);
} 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.storageLocationAreaEmptyID'), 'field'=>'notice']);
$areaObj = self::find($request->get->id);
if(is_null($areaObj))
return new Data(['success'=>false, 'message'=>L('error.storageLocationAreaNotFound'), 'field'=>'notice']);
// form check
if (!isset($request->post->storageLocationID) || empty($request->post->storageLocationID))
return new Data(['success'=>false, 'message'=>L('error.storageLocationAreaEmptyLocation'), 'field'=>'storageLocationID']);
if (!isset($request->post->name) || empty($request->post->name))
return new Data(['success'=>false, 'message'=>L('error.storageLocationAreaEmptyName'), 'field'=>'name']);
$editFields = [];
$editValues = [];
if (isset($request->post->storageLocationID) && !empty($request->post->storageLocationID)) {
$editFields['storageLocationID'] = "?";
$editValues[] = $request->post->storageLocationID;
}
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('storage_location_area')->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('storageLocationArea.storageLocation')."</th>";
$htmlContent .= "<th>".L('storageLocationArea.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('storageLocationArea.storageLocation')."</th>";
$htmlContent .= "<th>".L('storageLocationArea.name')."</th>";
$htmlContent .= "<th>".L('Status')."</th>";
$htmlContent .= "<th></th>";
$htmlContent .= "</tr>";
$htmlContent .= "</tfoot>";
return $htmlContent;
}
public static function genTableContentData() {
$sql = Sql::select(['storage_location_area', 'storageLocationArea'])->leftJoin(['storage_location', 'storageLocation'], "storageLocationArea.storageLocationID = storageLocation.id")->leftJoin(['status', 'status'], "storageLocationArea.status = status.id");
$sql->setFieldValue('
storageLocationArea.id id,
storageLocationArea.storageLocationID storageLocationID,
storageLocationArea.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>".storageLocation::find($listObj['storageLocationID'])->name."</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;
}
}