first commit
This commit is contained in:
104
admin/vats.php
Normal file
104
admin/vats.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
class Vats extends Window
|
||||
{
|
||||
protected $nameField = 'descr';
|
||||
|
||||
public function get_vars()
|
||||
{
|
||||
$vars = parent::get_vars();
|
||||
$pageVars = getVal('body', $vars);
|
||||
|
||||
$vars['body'] = $pageVars;
|
||||
|
||||
$this->unserializeCustomData($vars['body']['data']);
|
||||
|
||||
return $vars;
|
||||
}
|
||||
|
||||
public function getData()
|
||||
{
|
||||
$data = parent::getData();
|
||||
$this->serializeCustomData($data);
|
||||
|
||||
if (!getVal('Submit') && $this->getAction() == 'add') {
|
||||
$data['is_default'] = 'N';
|
||||
} else {
|
||||
if (!is_numeric($data['vat'])) {
|
||||
$data['vat'] = 0;
|
||||
}
|
||||
$data['vat'] = str_replace(',', '.', $data['vat']);
|
||||
}
|
||||
|
||||
if (findModule(Modules::OSS_VATS) && getVal('Submit') && $data['is_default'] == 'Y') {
|
||||
$defaultExists = sqlQueryBuilder()->select('id')->from('vats')
|
||||
->where(\Query\Operator::equalsNullable(['id_country' => $data['id_country']]))
|
||||
->andWhere(\Query\Operator::equals([
|
||||
'automanaged' => 1,
|
||||
'is_default' => 'Y',
|
||||
]))->execute()->fetchColumn();
|
||||
|
||||
if ($defaultExists) {
|
||||
$data['is_default'] = 'N';
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function handleUpdate()
|
||||
{
|
||||
$ID = $this->getID();
|
||||
if (findModule(\Modules::OSS_VATS) && $this->getAction() == 'edit') {
|
||||
$oldRow = sqlQueryBuilder()->select('*')->from('vats')
|
||||
->where(\Query\Operator::equals(['id' => $ID]))
|
||||
->execute()->fetch();
|
||||
if ($oldRow['automanaged'] ?? false) {
|
||||
$this->unserializeCustomData($oldRow);
|
||||
$rawData = parent::getData();
|
||||
$newCustomData = array_merge($oldRow['data'] ?? [], $rawData['data']);
|
||||
sqlQueryBuilder()->update('vats')->where(\Query\Operator::equals(['id' => $ID]))
|
||||
->directValues(['data' => json_encode($newCustomData)])
|
||||
->execute();
|
||||
// skip other changes for automanaged rows
|
||||
$this->returnOK();
|
||||
}
|
||||
}
|
||||
$data = $this->getData();
|
||||
|
||||
if ($data['is_default'] == 'Y') {
|
||||
$qbUpdate = sqlQueryBuilder()->update('vats')
|
||||
->set('is_default', "'N'")
|
||||
->where("is_default = 'Y'");
|
||||
|
||||
if (findModule(Modules::OSS_VATS)) {
|
||||
$qbUpdate->andWhere(\Query\Operator::equalsNullable(['id_country' => $data['id_country'] ?: null]))
|
||||
->andWhere(\Query\Operator::equals(['automanaged' => 0]));
|
||||
}
|
||||
$qbUpdate->execute();
|
||||
}
|
||||
|
||||
return parent::handleUpdate();
|
||||
}
|
||||
|
||||
public function hasRights($name = null)
|
||||
{
|
||||
if ($name == Window::RIGHT_DELETE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (findModule(Modules::OSS_VATS)) {
|
||||
$body = getVal('body', parent::get_vars());
|
||||
if (($body['data']['automanaged'] ?? 0)
|
||||
&& in_array($name, [static::RIGHT_DUPLICATE, static::RIGHT_DELETE])
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::hasRights($name);
|
||||
}
|
||||
}
|
||||
|
||||
$vats = new Vats();
|
||||
$vats->run();
|
||||
Reference in New Issue
Block a user