Files
2025-08-02 16:30:27 +02:00

129 lines
4.0 KiB
PHP

<?php
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\WarehouseBundle\Entity\StoreItem;
$main_class = 'Positions';
class Positions extends Window
{
protected $tableName = 'warehouse_positions';
protected $nameField = 'code';
public function get_vars()
{
$vars = parent::get_vars();
$vars['body']['locations'] = sqlQueryBuilder()->select('*')->from('warehouse_locations')->execute()->fetchAll();
if (findModule(Modules::STORES)) {
$vars['body']['store_transfer'] = sqlQueryBuilder()->select('*')->from('stores_transfers')->andWhere(\Query\Operator::equals(['id_position' => $this->getID()]))->execute()->fetch();
}
$vars['body']['order'] = sqlQueryBuilder()->select('o.order_no as order_no, o.id')->from('warehouse_orders', 'wo')
->join('wo', 'orders', 'o', 'o.id = wo.id_order')
->andWhere(\Query\Operator::equals(['id_position' => $this->getID()]))->execute()->fetch();
return $vars;
}
public function getData()
{
$data = parent::getData();
$acn = $this->getAction();
if ($acn == 'add' && getVal('Submit')) {
$locationName = sqlQueryBuilder()->select('code')->from('warehouse_locations')->where(\Query\Operator::equals(['id' => $data['id_location']]))->execute()->fetchColumn();
$data['code'] = $locationName.'-'.$data['code'];
}
return $data;
}
public function handleDelete()
{
if (!findRight('LOC_DEL')) {
$this->returnError('Na mazání nemáte práva');
}
if (sqlQueryBuilder()->select('*')->from('warehouse_products')
->where(\Query\Operator::equals(['id_position' => $this->getID()]))
->andWhere('pieces > 0')
->execute()->fetchAll()) {
$this->returnError('Nelze! Na pozici jsou produkty!');
}
parent::handleDelete();
}
public function handlePrint()
{
$qb = sqlQueryBuilder()->select('*')
->from('warehouse_positions', 'wp');
$positions = getVal('positions');
if ($positions) {
$qb->andWhere(\Query\Operator::inIntArray($positions, 'id'));
}
$location = getVal('location');
if ($location) {
$qb->andWhere(\Query\Operator::equals(['id_location' => $location]));
}
$smarty = createSmarty(true, true);
$smarty->assign([
'positions' => $qb->execute(),
]);
$smarty->display('print/position.tpl');
exit;
}
public function handleEditProducts()
{
if (!findRight('WAR_PROD_EDIT')) {
$this->returnError('! Na takový úkon nemáte právo !');
}
$data = parent::getData();
$ID = $this->getID();
if (empty($data['id_product']) || empty($data['pieces'])) {
$this->returnError('Chybí vybraný produkt nebo kusy!');
}
sqlGetConnection()->transactional(function () use ($ID, $data) {
$storeItemWorker = ServiceContainer::getService(\KupShop\WarehouseBundle\Util\StoreItemWorker::class);
if (empty($data['id_variation'])) {
$data['id_variation'] = null;
}
if (empty($data['id_product_batch'])) {
$data['id_product_batch'] = null;
}
$storeItemWorker->createStoreItem(new StoreItem($data), $data['pieces'], $ID, ['handle_added' => true], true);
});
$this->returnOK('Přidáno. A zalogováno !!');
}
public function hasRights($name = null)
{
switch ($name) {
case Window::RIGHT_DELETE:
return findRight('LOC_DEL');
case Window::RIGHT_DUPLICATE:
case Window::RIGHT_SAVE:
if ($this->getAction() == 'add') {
return parent::hasRights($name);
}
return false;
default:
return parent::hasRights($name);
}
}
}