File: /var/www/html/inventory.breadsecret.com/class/Controller/item.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\supplier, Controller\itemType, Controller\formLayout;
class item implements Listable {
private $stmStatus = null;
private $stmItemType = null;
private $stmUnit = null;
public static function find($id, $fetchMode=\PDO::FETCH_OBJ) {
$sql = Sql::select("item")->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("item")->where(['status', '=', "1"]);
$stm = $sql->prepare();
$stm->execute();
return $stm;
}
public static function totalValue($fetchMode=\PDO::FETCH_OBJ) {
$sum = 0;
$sql = Sql::select("item")->where(['status', '=', "1"]);
$stm = $sql->prepare();
$stm->execute();
foreach($stm as $item) {
$sum += $item['totalValue'];
}
return $sum;
}
public static function totalQty($fetchMode=\PDO::FETCH_OBJ) {
$sum = 0;
$sql = Sql::select("item")->where(['status', '=', "1"]);
$stm = $sql->prepare();
$stm->execute();
foreach($stm as $item) {
$sum += $item['qty'];
}
return $sum;
}
public static function totalValueByItemType($itemTypeID, $fetchMode=\PDO::FETCH_OBJ) {
$sum = 0;
$sql = Sql::select("item")->where(['status', '=', "1"])->where(['itemTypeID', '=', $itemTypeID]);
$stm = $sql->prepare();
$stm->execute();
foreach($stm as $item) {
$sum += $item['totalValue'];
}
return $sum;
}
public static function totalQtyByItemType($itemTypeID, $fetchMode=\PDO::FETCH_OBJ) {
$sum = 0;
$sql = Sql::select("item")->where(['status', '=', "1"])->where(['itemTypeID', '=', $itemTypeID]);
$stm = $sql->prepare();
$stm->execute();
foreach($stm as $item) {
$sum += $item['qty'];
}
return $sum;
}
public static function totalValueByStorageLocation($locationID, $fetchMode=\PDO::FETCH_OBJ) {
$sum = 0;
$sql = Sql::select(['item_stock', 'stock'])
->leftJoin(['storage_location_area', 'area'], "stock.storageLocationAreaID = area.id")
->leftJoin(['storage_location', 'location'], "area.storageLocationID = location.id")->where(['location.id', '=', $locationID])->where(['stock.status', '=', 1]);
$sql->setFieldValue('
stock.id id,
area.name areaName,
location.name locationName,
stock.qty stockQty,
stock.totalValue stockValue
');
$stm = $sql->prepare();
$stm->execute();
foreach($stm as $item) {
$sum += $item['stockValue'];
}
return $sum;
}
public static function getItemTypeStatistic() {
$data = [];
$itemTypeList = itemType::findAll();
foreach($itemTypeList as $itemType) {
$data[$itemType['name']] = self::totalValueByItemType($itemType['id']);
}
return $data;
}
public static function getLocationStatistic() {
$data = [];
$locationList = storageLocation::findAll();
foreach($locationList as $location) {
$data[$location['name']] = self::totalValueByStorageLocation($location['id']);
}
return $data;
}
public static function getDetail($request, $fetchMode=\PDO::FETCH_OBJ) {
$sql = Sql::select("item")->where(['id', '=', $request->get->id]);
$stm = $sql->prepare();
$stm->execute();
$obj = $stm->fetch($fetchMode);
if ($obj === false) return new Data(['success'=>false, 'message'=>L('error.itemNotFound')]);
$obj->supplier = supplier::find($obj->supplierID)->name;
$obj->itemType = itemType::find($obj->itemTypeID)->name;
$obj->unit = unit::find($obj->unitID)->name;
return new Data(['success'=>true, 'message'=>json_encode($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.itemEmptyID')]);
$itemObj = self::find($request->get->id);
if(is_null($itemObj)) return new Data(['success'=>false, 'message'=>L('error.itemNotFound')]);
$itemTypeObj = itemType::find($itemObj->itemTypeID);
$supplierObj = supplier::find($itemObj->supplierID);
$unitObj = unit::find($itemObj->unitID);
$content = "<div class='row'>";
/*
$content .= formLayout::rowDisplayLineNew(L('item.name'),$itemObj->name);
$content .= formLayout::rowDisplayLineNew(L('item.description'),$itemObj->description);
$content .= formLayout::rowDisplayLineNew(L('item.type'),$itemTypeObj->name);
$content .= formLayout::rowDisplayLineNew(L('item.supplier'),$supplierObj->name);
$content .= formLayout::rowDisplayLineNew(L('item.qty'),$itemObj->qty);
$content .= formLayout::rowDisplayLineNew(L('item.unit'),$unitObj->name);
$content .= formLayout::rowDisplayLineNew(L('item.cost'),$itemObj->totalValue);
*/
$content .= formLayout::rowInputNew(L('item.type'),'', '', 'text', 6, [], ['disabled'], $itemTypeObj->name);
$content .= formLayout::rowInputNew(L('item.name'),'', '', 'text', 6, [], ['disabled'], $itemObj->name);
$content .= formLayout::rowInputNew(L('item.description'),'', '', 'text', 12, [], ['disabled'], $itemObj->description);
$content .= formLayout::rowInputNew(L('item.supplier'),'', '', 'text', 6, [], ['disabled'], $supplierObj->name);
$content .= formLayout::rowInputNew(L('item.qty'),'', '', 'text', 6, [], ['disabled'], $itemObj->qty);
$content .= formLayout::rowInputNew(L('item.unit'),'', '', 'text', 6, [], ['disabled'], $unitObj->name);
$content .= formLayout::rowInputNew(L('item.cost'),'', '', 'text', 6, [], ['disabled'], $itemObj->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('stock.storageLocation')."</th>";
$content .= "<th>".L('stock.batchNo')."</th>";
$content .= "<th>".L('stock.qty')."</th>";
$content .= "<th>".L('stock.purchaseUnitCost')."</th>";
$content .= "<th>".L('stock.value')."</th>";
$content .= "</tr>";
$content .= "</thead>";
$sql = Sql::select(['item_stock', 'stock'])
->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(['stockDetail.qty', '>', 0])->where(['stock.itemID', '=', $request->get->id]);
$sql->setFieldValue('
stockDetail.id id,
area.name areaName,
location.name locationName,
stockDetail.batchNo batchNo,
stockDetail.qty qty,
stockDetail.purchaseUnitCost purchaseUnitCost
');
$stm = $sql->prepare();
$stm->execute();
$obj = $stm->fetchAll($fetchMode);
if ($obj === false) return new Data(['success'=>false, 'message'=>L('error.itemNotFound')]);
$data = array();
foreach($obj as $stockDetail){
$content .= "<tr>";
$content .= "<td>".$stockDetail->locationName." - ".$stockDetail->areaName."</td>";
$content .= "<td>".$stockDetail->batchNo."</td>";
$content .= "<td>".$stockDetail->qty."</td>";
$content .= "<td>".$stockDetail->purchaseUnitCost."</td>";
$content .= "<td>".$stockDetail->qty*$stockDetail->purchaseUnitCost."</td>";
$content .= "</tr>";
}
$content .= "</table></div>";
return new Data(['success'=>true, 'message'=>$content]);
}
public static function getStockStorageLocationArea($request, $fetchMode=\PDO::FETCH_OBJ) {
$sql = Sql::select(['item_stock_detail', 'stockDetail'])
->leftJoin(['item_stock', 'stock'], "stockDetail.itemStockID = stock.id")
->leftJoin(['storage_location_area', 'area'], "stock.storageLocationAreaID = area.id")
->leftJoin(['storage_location', 'location'], "area.storageLocationID = location.id")->where(['stock.qty', '>', 0])->where(['stock.itemID', '=', $request->get->id])->where(['1', 'GROUP BY', 'area.id']);
$sql->setFieldValue('
stock.id id,
area.name areaName,
location.name locationName
');
$stm = $sql->prepare();
$stm->execute();
$obj = $stm->fetchAll($fetchMode);
if ($obj === false) return new Data(['success'=>false, 'message'=>L('error.itemNotFound')]);
$data = array();
foreach($obj as $area){
$data[]=["id"=>$area->id, "areaName"=>$area->areaName, "locationName"=>$area->locationName];
}
return new Data(['success'=>true, 'message'=>json_encode($data)]);
}
public function extraProcess($listObj) {
if (is_null($this->stmItemType))
$this->stmItemType = Sql::select('item_type')->where(['id', '=', "?"])->prepare();
$this->stmItemType->execute([$listObj->itemTypeID]);
$objItemType = $this->stmItemType->fetch();
$listObj->itemType = $objItemType['name'];
if (is_null($this->stmUnit))
$this->stmUnit = Sql::select('unit')->where(['id', '=', "?"])->prepare();
$this->stmUnit->execute([$listObj->unitID]);
$objUnitType = $this->stmUnit->fetch();
$listObj->unit = $objUnitType['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('item/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.itemEmptyID')]);
$sql = Sql::delete('item')->where(['id', '=', $request->get->id]);
if ($sql->prepare()->execute()) {
return new Data(['success'=>true, 'message'=>L('info.itemDeleted')]);
} else {
return new Data(['success'=>false, 'message'=>L('error.itemDeleteFailed')]);
}
}
public function itemForm($request) {
if (!user::checklogin()) return new Data(['success'=>false, 'message'=>L('login.signInMessage'), 'note'=>'signIn']);
$obj = null;
if (isset($request->get->id))
$obj = self::find($request->get->id, \PDO::FETCH_NAMED);
$formName = "form-addItem";
$viewMode = isset($request->get->view);
if(!is_null($obj)) {
if(isset($request->get->view)){
$formName = "form-viewItem";
}
else{
$formName = "form-editItem";
}
}
$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.itemAddHelperMessage')."</p></div>";
$content .= "<div class='row'>";
$option = [""=>"", "Add"=>"[".L('Add')."]"];
$stm = Sql::select('item_type')->where(['status', '=', 1])->prepare();
$stm->execute();
foreach ($stm as $opt) {
$option[$opt['id']] = $opt['name'];
}
$content .= formLayout::rowSelectNew(L('item.type'), 'itemTypeID', 'itemTypeID', $option, 6, [], [$viewMode ? 'disabled' : 'required'], is_null($obj)?'':$obj['itemTypeID']);
//$content .= formLayout::rowRadioNew(L('item.type'), 'itemTypeID', 'itemTypeID', $option, 12, [], ['required'], is_null($obj)?'1':$obj['itemTypeID']);
$content .= formLayout::rowInputNew(L('item.name'),'name', 'name', 'text', 6, [], [$viewMode ? 'disabled' : 'required'], is_null($obj)?'':$obj['name']);
$content .= formLayout::rowTextAreaNew(L('item.description'), 'description', 'description', 12, [], [$viewMode ? 'disabled' : ''], is_null($obj)?'':$obj['description']);
$option = [""=>"", "Add"=>"[".L('Add')."]"];
$stm = Sql::select('supplier')->where(['status', '=', 1])->prepare();
$stm->execute();
foreach ($stm as $opt) {
$option[$opt['id']] = $opt['name'];
}
$content .= formLayout::rowSelectNew(L('item.supplier'), 'supplierID', 'supplierID', $option, 6, [], [$viewMode ? 'disabled' : 'required'], is_null($obj)?'':$obj['supplierID']);
//$content .= formLayout::rowInputNew(L('item.qty'),'qty', 'qty', 'text', 12, [], ['required'], is_null($obj)?'':$obj['name']);
$option = [""=>"", "Add"=>"[".L('Add')."]"];
$stm = Sql::select('unit')->where(['status', '=', 1])->prepare();
$stm->execute();
foreach ($stm as $opt) {
$option[$opt['id']] = $opt['name'];
}
//$content .= formLayout::rowRadioNew(L('item.unit'), 'unitID', 'unitID', $option, 12, [], ['required'], is_null($obj)?'1':$obj['unitID']);
$content .= formLayout::rowSelectNew(L('item.unit'), 'unitID', 'unitID', $option, 6, [], [$viewMode ? 'disabled' : 'required'], is_null($obj)?'':$obj['unitID']);
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, 6, [], [$viewMode ? 'disabled' : 'required'], is_null($obj)?'':$obj['status']);
}
$content .= "</div></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']);
// form check
if (!isset($request->post->itemTypeID) || empty($request->post->itemTypeID))
return new Data(['success'=>false, 'message'=>L('error.itemEmptyType'), 'field'=>'itemTypeID']);
if (!isset($request->post->name) || empty($request->post->name))
return new Data(['success'=>false, 'message'=>L('error.itemEmptyName'), 'field'=>'name']);
/*
if (!isset($request->post->description) || empty($request->post->description))
return new Data(['success'=>false, 'message'=>L('error.itemEmptyDescription'), 'field'=>'description']);
*/
if (!isset($request->post->supplierID) || empty($request->post->supplierID))
return new Data(['success'=>false, 'message'=>L('error.itemEmptySupplier'), 'field'=>'supplierID']);
if (!isset($request->post->unitID) || empty($request->post->unitID))
return new Data(['success'=>false, 'message'=>L('error.itemEmptyUnit'), 'field'=>'unitID']);
// insert database
$sql = Sql::insert('item')->setFieldValue([
'itemTypeID' => "?",
'name' => "?",
'description' => "?",
'supplierID' => "?",
'unitID' => "?"
]);
if ($sql->prepare()->execute([
strip_tags($request->post->itemTypeID),
strip_tags($request->post->name),
strip_tags($request->post->description),
strip_tags($request->post->supplierID),
strip_tags($request->post->unitID)
])) {
$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']);
if (!isset($request->get->id) || empty($request->get->id))
return new Data(['success'=>false, 'message'=>L('error.itemEmptyID'), 'field'=>'notice']);
$itemObj = self::find($request->get->id);
if(is_null($itemObj))
return new Data(['success'=>false, 'message'=>L('error.itemNotFound'), 'field'=>'notice']);
if (!isset($request->post->itemTypeID) || empty($request->post->itemTypeID))
return new Data(['success'=>false, 'message'=>L('error.itemEmptyType'), 'field'=>'itemTypeID']);
if (!isset($request->post->name) || empty($request->post->name))
return new Data(['success'=>false, 'message'=>L('error.itemEmptyName'), 'field'=>'name']);
/*
if (!isset($request->post->description) || empty($request->post->description))
return new Data(['success'=>false, 'message'=>L('error.itemEmptyDescription'), 'field'=>'description']);
*/
if (!isset($request->post->supplierID) || empty($request->post->supplierID))
return new Data(['success'=>false, 'message'=>L('error.itemEmptySupplier'), 'field'=>'supplierID']);
if (!isset($request->post->unitID) || empty($request->post->unitID))
return new Data(['success'=>false, 'message'=>L('error.itemEmptyUnit'), 'field'=>'unitID']);
$editFields = [];
$editValues = [];
if (isset($request->post->itemTypeID) && !empty($request->post->itemTypeID)) {
$editFields['itemTypeID'] = "?";
$editValues[] = $request->post->itemTypeID;
}
if (isset($request->post->name) && !empty($request->post->name)) {
$editFields['name'] = "?";
$editValues[] = $request->post->name;
}
if (isset($request->post->description) && !empty($request->post->description)) {
$editFields['description'] = "?";
$editValues[] = $request->post->description;
}
if (isset($request->post->supplierID) && !empty($request->post->supplierID)) {
$editFields['supplierID'] = "?";
$editValues[] = $request->post->supplierID;
}
if (isset($request->post->unitID) && !empty($request->post->unitID)) {
$editFields['unitID'] = "?";
$editValues[] = $request->post->unitID;
}
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('item')->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('item.type')."</th>";
$htmlContent .= "<th>".L('item.name')."</th>";
$htmlContent .= "<th>".L('item.supplier')."</th>";
$htmlContent .= "<th>".L('item.unit')."</th>";
$htmlContent .= "<th>".L('item.qty')."</th>";
$htmlContent .= "<th>".L('item.cost')."</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('item.type')."</th>";
$htmlContent .= "<th>".L('item.name')."</th>";
$htmlContent .= "<th>".L('item.supplier')."</th>";
$htmlContent .= "<th>".L('item.unit')."</th>";
$htmlContent .= "<th>".L('item.qty')."</th>";
$htmlContent .= "<th>".L('item.cost')."</th>";
$htmlContent .= "<th>".L('Status')."</th>";
$htmlContent .= "<th></th>";
$htmlContent .= "</tr>";
$htmlContent .= "</tfoot>";
return $htmlContent;
}
public static function genTableContentData() {
$sql = Sql::select(['item', 'item'])->leftJoin(['supplier', 'supplier'], "item.supplierID = supplier.id")->leftJoin(['unit', 'unit'], "item.unitID = unit.id")->leftJoin(['item_type', 'itemType'], "item.itemTypeID = itemType.id")->leftJoin(['status', 'status'], "item.status = status.id");
$sql->setFieldValue('
item.id id,
item.itemTypeID itemTypeID,
item.name name,
item.supplierID supplierID,
item.description description,
item.unitID unitID,
item.qty qty,
item.totalValue totalValue,
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>".itemType::find($listObj['itemTypeID'])->name."</td>";
$htmlContent .= "<td>".$listObj['name']."</td>";
$htmlContent .= "<td>".supplier::find($listObj['supplierID'])->name."</td>";
$htmlContent .= "<td>".unit::find($listObj['unitID'])->name."</td>";
$htmlContent .= "<td>".$listObj['qty']."</td>";
$htmlContent .= "<td>".$listObj['totalValue']."</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;
}
}