263 lines
8.5 KiB
PHP
263 lines
8.5 KiB
PHP
<?php
|
|
|
|
namespace BRSystems\Core\Cms\cmsBuys\inc;
|
|
|
|
use BRSystems\Core\Cms\cmsOrders\inc\cmsOrdersController;
|
|
use BRSystems\Core\Cms\cmsProducts\inc\cmsProductsModule;
|
|
use BRSystems\Core\Library\cCore\cCore;
|
|
use BRSystems\Core\Library\cJSON\cJSON;
|
|
use BRSystems\Core\Library\cValidator\cValidator;
|
|
|
|
class cmsBuysController extends cCore
|
|
{
|
|
public $dir;
|
|
public $name;
|
|
public $module;
|
|
|
|
public function __construct($uriData, $dir, $name)
|
|
{
|
|
$this->dir = $dir;
|
|
$this->name = $name;
|
|
|
|
parent:: __construct($uriData);
|
|
$this->module = new cmsBuysModule();
|
|
|
|
$this->oView->setTemplateDir($this->dir . "/tmpl/");
|
|
|
|
if (!$this->oView->cacheUsed()) {
|
|
$this->oView->setJs(getCurrentFolder($this->dir) . '/js/' . $this->name . '.js');
|
|
$this->oView->setCss(getCurrentFolder($this->dir) . '/css/' . $this->name . '.css');
|
|
}
|
|
|
|
// Set JS module init title
|
|
$this->oView->setAsJSON(cCore::JS_MOD, ucfirst($name));
|
|
}
|
|
|
|
public function run($initEditMode = false): void
|
|
{
|
|
$this->oView->TABLE = true;
|
|
|
|
$this->oView->setAsJSON('initEdit', $initEditMode);
|
|
$this->oView->setAsJSON('baseUrl', $this->gL());
|
|
$this->oView->setAsJSON('gL', $this->getJsTranslationsForView());
|
|
$this->oView->setAsJSON('vcfg', $this->getValCfg());
|
|
|
|
$this->oView->MODULE_TITLE = $this->gM("Products sold");
|
|
$this->oView->PARTNERS = $this->module->getPartners();
|
|
|
|
$this->oView->INS_DATE = date($this->CMSDateFormat, time());
|
|
|
|
$this->oView->display('table', 'CMS_CUSTOM_TPL_DATA');
|
|
}
|
|
|
|
public function getDropDownData($data): array
|
|
{
|
|
$data = json_decode($data ?? '', true);
|
|
if (empty($data) || !is_array($data)) {
|
|
return ['data' => []];
|
|
}
|
|
|
|
$sWhere = '';
|
|
foreach ($data as $key => $val) {
|
|
if (!strlen($val)) {
|
|
continue;
|
|
}
|
|
|
|
$sVal = $this->module->db->prepData($val, true);
|
|
switch ($key) {
|
|
case 'search_id':
|
|
$sWhere .= " AND mp.prod_nr LIKE '%{$sVal}%'";
|
|
break;
|
|
|
|
case 'search_title':
|
|
$sWhere .= " AND mpi.prod_name LIKE '%{$sVal}%'";
|
|
break;
|
|
case 'search_partner':
|
|
$sWhere .= " AND mp.prod_comp_link_id = {$sVal}";
|
|
break;
|
|
}
|
|
}
|
|
|
|
return ['data' => $this->module->getDataBySearch($sWhere)];
|
|
}
|
|
|
|
public function getProductsByPartnerId($id): array
|
|
{
|
|
$where = " AND mp.prod_comp_link_id = " . $this->module->db->prepData($id);
|
|
return ['data' => $this->module->getDataBySearch($where)];
|
|
}
|
|
|
|
public function loadClassification($id): array
|
|
{
|
|
return [
|
|
'html' => $this->oView->genTemplate('data/classificationData.tpl'),
|
|
'data' => $this->module->getProdDivision($id),
|
|
];
|
|
}
|
|
|
|
public function saveRecordData($recordId, $fields)
|
|
{
|
|
$fields = cJSON::decode($fields ?? '', true);
|
|
if (empty($fields)) {
|
|
cJSON::senderror(['data' => $this->gM("No data to save")]);
|
|
}
|
|
|
|
// Validate fields
|
|
$valid = new cValidator($this);
|
|
$errors = $valid->checkArray($fields, $this->getValCfg(true));
|
|
if ($errors !== true) {
|
|
cJSON::senderror(['data' => $errors]);
|
|
}
|
|
|
|
$date = $fields['date'];
|
|
unset($fields['date']);
|
|
|
|
if (empty($fields) || !is_array($fields)) {
|
|
cJSON::senderror(['data' => $this->gM("No data to save")]);
|
|
}
|
|
|
|
$vToCount = [];
|
|
|
|
// Get all old linked count to class
|
|
$vCC = $this->module->getProdDivision($recordId);
|
|
$iTime = time();
|
|
foreach ($fields as $k => $v) {
|
|
$oldKey = $k;
|
|
$k = substr($k, 1);
|
|
$rid = $v["id{$k}"] ?: 0;
|
|
$c = ($v["count{$k}"] > 0 ? ($v["count{$k}"] * -1) : $v["count{$k}"]);
|
|
if ($c != 0 && $rid && !!$vCC[$rid]) {
|
|
$vToCount[$oldKey] = [
|
|
'prod_id' => $recordId,
|
|
'row_id' => $rid,
|
|
'cl1_id' => $vCC[$rid]['prod_cl_id'],
|
|
'cl2_id' => $vCC[$rid]['prod_cl_id_2'],
|
|
'count' => $c,
|
|
'price_sold' => $v["cprice{$k}"],
|
|
'note' => (!empty($v["notes{$k}"]) ? $v["notes{$k}"] : ''),
|
|
'op_type' => cmsOrdersController::PRODUCT_OPERATION_BUYS_MODULE,
|
|
'op_date' => $iTime,
|
|
'user_id' => $this->oUser->getUser(),
|
|
'date' => $date,
|
|
'modified' => $iTime,
|
|
'modified_by' => $this->oUser->getUser(),
|
|
'created' => $iTime,
|
|
'created_by' => $this->oUser->getUser(),
|
|
];
|
|
}
|
|
}
|
|
|
|
// IF we check product availability, send error if product count is less then zero
|
|
if (cmsOrdersController::PRODUCT_AVAILABILITY_CHK) {
|
|
if ($this->checkProductAvailability($recordId, $vToCount)) {
|
|
$errors = [];
|
|
foreach ($vToCount as $k => $v) {
|
|
if ($v['error']) {
|
|
$errors[$k]['count' . substr($k, 1)] = $this->gM("Product count unavailable");
|
|
}
|
|
}
|
|
|
|
cJSON::senderror(['data' => $errors]);
|
|
}
|
|
}
|
|
|
|
|
|
// Insert count
|
|
if ($this->module->insertMultiArray(cmsProductsModule::TABLE_TO_COUNT, $vToCount)) {
|
|
cJSON::sendok();
|
|
}
|
|
|
|
cJSON::senderror(['data' => $this->gM("Error saving data")]);
|
|
}
|
|
|
|
public function getJsTranslationsForView(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Function to get js validator cfg
|
|
*
|
|
* @param bool $bPrepDataCfg - reconfigure cfg data array
|
|
* @return array of data
|
|
* @author Aivars Žukovskis <keiss.mareks@gmail.com>
|
|
* @category cmsBuys Controller
|
|
*/
|
|
public function getValCfg($bPrepDataCfg = true): array
|
|
{
|
|
$valCfg = [
|
|
|
|
// Additional validation config
|
|
'cfg' => [
|
|
cValidator::CFG_BIND_FIELD => 'data-validator',
|
|
],
|
|
|
|
'date' => [
|
|
cValidator::P_TYPE => cValidator::TY_DATE,
|
|
cValidator::P_COMP => 1,
|
|
cValidator::P_TEST => [cValidator::CHK_DATE],
|
|
cValidator::P_EVENT => 'change',
|
|
cValidator::P_ADDS => ['2unix' => true],
|
|
],
|
|
'count' => [
|
|
cValidator::P_TYPE => cValidator::TY_DEC,
|
|
cValidator::P_COMP => 1,
|
|
cValidator::P_TEST => [cValidator::CHK_NUM],
|
|
],
|
|
'cprice' => [
|
|
cValidator::P_TYPE => cValidator::TY_DEC,
|
|
cValidator::P_COMP => 0,
|
|
cValidator::P_TEST => [cValidator::CHK_NUM],
|
|
],
|
|
/*'iprice' => array(
|
|
cValidator::P_TYPE => cValidator::TY_DEC,
|
|
cValidator::P_COMP => 0,
|
|
cValidator::P_TEST => array(cValidator::CHK_NUM),
|
|
),
|
|
'cprice' => array(
|
|
cValidator::P_TYPE => cValidator::TY_STR,
|
|
cValidator::P_COMP => 0,
|
|
cValidator::P_TEST => array(cValidator::CHK_STR),
|
|
),*/
|
|
];
|
|
|
|
return ($bPrepDataCfg ? cValidator::prepValCfgForValidation($valCfg) : $valCfg);
|
|
}
|
|
|
|
/**
|
|
* Function to check product availability on save
|
|
*
|
|
* @param int $id - product id
|
|
* @param array $vProdToCount - products to count array by reference
|
|
* @return bool true/false if there is error or not
|
|
* @category cmsBuys Controller
|
|
* @author Aivars Žukovskis <keiss.mareks@gmail.com>
|
|
*/
|
|
public function checkProductAvailability($id, &$vProdToCount): bool
|
|
{
|
|
$bError = false;
|
|
$vData = $this->module->getProdDivision($id);
|
|
foreach ($vProdToCount as &$v) {
|
|
if (!empty($vData[$v['row_id']])) {
|
|
continue;
|
|
}
|
|
|
|
$vProdRow = &$vData[$v['row_id']];
|
|
if ($vProdRow['c'] > 0) {
|
|
if ($vProdRow['c'] + $v['count'] < 0) {
|
|
$bError = true;
|
|
$v['error'] = 1;
|
|
}
|
|
} else {
|
|
$bError = true;
|
|
$v['error'] = 1;
|
|
}
|
|
|
|
unset($vProdRow);
|
|
}
|
|
unset($v);
|
|
|
|
return $bError;
|
|
}
|
|
}
|