204 lines
7.0 KiB
PHP
204 lines
7.0 KiB
PHP
|
|
<?
|
||
|
|
namespace shop\order;
|
||
|
|
|
||
|
|
use core\db\structure\Column as C;
|
||
|
|
use core\db\structure\eColumnType;
|
||
|
|
use shop\Order;
|
||
|
|
use ui\form\structure\eInputType;
|
||
|
|
use ui\Form;
|
||
|
|
use ui\input\Input;
|
||
|
|
use core\lang\structure\LangVariable as V;
|
||
|
|
use ms\ms\structure\msModuleTable;
|
||
|
|
use shop\order\cashback\structure\shopOrderCashbackLang as Vars;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
class Cashback extends msModuleTable {
|
||
|
|
static $self;
|
||
|
|
static $table_name = 'shopOrderCashback';
|
||
|
|
static $controller = 'shop.order.cashback';
|
||
|
|
|
||
|
|
static $ID = 'id';
|
||
|
|
static $PROFILE = 'profile';
|
||
|
|
static $ORDER = 'order';
|
||
|
|
static $VALUE = 'value';
|
||
|
|
static $ACCOUNT = 'account';
|
||
|
|
static $CREATED = 'created';
|
||
|
|
static $UPDATED = 'updated';
|
||
|
|
|
||
|
|
|
||
|
|
static function getTitle(){return V::get(Vars::$MODULE_NAME);}
|
||
|
|
static function getSystemLangValues():array{ return Vars::getArray(); }
|
||
|
|
|
||
|
|
public function _update(\Where $w, array $values) {
|
||
|
|
$values[self::$UPDATED] = time();
|
||
|
|
return parent::_update($w, $values);
|
||
|
|
}
|
||
|
|
static function getPotentialSumByOrder($order_id){
|
||
|
|
$order_id = intval($order_id);
|
||
|
|
$r = self::select([
|
||
|
|
\Query::SELECT => ['SUM('.self::$VALUE.') as cc'],
|
||
|
|
\Query::WHERE => new \Where(\Where::_and([
|
||
|
|
\Where::_operator(self::$ORDER,'=',$order_id),
|
||
|
|
\Where::_operator(self::$VALUE,'>',0),
|
||
|
|
]))
|
||
|
|
]);
|
||
|
|
$l = \DB::fetch($r);
|
||
|
|
return cfloatval($l['cc']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function _deleteByIDS(array $ids) {
|
||
|
|
$a = self::getByIDS($ids);
|
||
|
|
$res = parent::_deleteByIDS($ids);
|
||
|
|
$profile_ids = [];
|
||
|
|
$orders_ids = [];
|
||
|
|
foreach ($a as $v){
|
||
|
|
$profile_ids[] = $v[Cashback::$PROFILE];
|
||
|
|
$orders_ids[] = $v[Cashback::$ORDER];
|
||
|
|
}
|
||
|
|
$profile_ids = array_unique($profile_ids);
|
||
|
|
$orders_ids = array_unique($orders_ids);
|
||
|
|
foreach ($profile_ids as $id) \Profile::remath($id);
|
||
|
|
foreach ($orders_ids as $id) Order::remath($id);
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
|
||
|
|
static function getByProfile($pid){
|
||
|
|
$pid = intval($pid);
|
||
|
|
return self::select([
|
||
|
|
\Query::LEFT_JOINS=>[
|
||
|
|
[Order::$table_name,Order::$ID,self::$ORDER],
|
||
|
|
],
|
||
|
|
\Query::WHERE => new \Where(\WHere::_and([
|
||
|
|
\Where::_operator(self::$PROFILE,'=',$pid)
|
||
|
|
]))
|
||
|
|
],true);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function _insert(array $values, array $options = array()) {
|
||
|
|
$res = parent::_insert($values, $options);
|
||
|
|
$C = self::getByID($res->id);
|
||
|
|
\Profile::remathCashback($C[self::$PROFILE]);
|
||
|
|
Order::remath($C[self::$ORDER]);
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
public static function afterUadd($values, $id, $result) {
|
||
|
|
$res = parent::afterUadd($values, $id, $result);
|
||
|
|
$C = self::getByID($id);
|
||
|
|
Order::remath($C[self::$ORDER]);
|
||
|
|
\Profile::remathCashback($C[self::$PROFILE]);
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function installUniques() {
|
||
|
|
\DB::addIndex(self::$table_name,self::$ORDER);
|
||
|
|
\DB::addIndex(self::$table_name,self::$PROFILE);
|
||
|
|
}
|
||
|
|
|
||
|
|
static function getForm($values = [], $options = []) :array {
|
||
|
|
return [
|
||
|
|
self::getCommonFormInputs(),
|
||
|
|
[
|
||
|
|
Form::VIEW =>Form::INPUT,
|
||
|
|
Form::KEY_VALUE =>self::$PROFILE,
|
||
|
|
Form::TYPE =>eInputType::HIDDEN,
|
||
|
|
],
|
||
|
|
[
|
||
|
|
Form::VIEW =>Form::INPUT,
|
||
|
|
Form::KEY_VALUE =>self::$ORDER,
|
||
|
|
Form::TYPE =>eInputType::HIDDEN,
|
||
|
|
],
|
||
|
|
[
|
||
|
|
Form::VIEW =>Form::INPUT,
|
||
|
|
Form::KEY_VALUE =>self::$ACCOUNT,
|
||
|
|
Form::TYPE =>eInputType::HIDDEN,
|
||
|
|
Form::INPUT_OPT => [
|
||
|
|
Input::VALUE => UID,
|
||
|
|
],
|
||
|
|
],
|
||
|
|
[
|
||
|
|
Form::VIEW=>Form::ROW,
|
||
|
|
Form::COLS => [
|
||
|
|
[
|
||
|
|
Form::VIEW =>Form::INPUT,
|
||
|
|
Form::COL_CLASSES =>'c c-1-3',
|
||
|
|
Form::KEY_VALUE =>self::$VALUE,
|
||
|
|
Form::TYPE =>eInputType::TEXT,
|
||
|
|
Form::INPUT_OPT => [
|
||
|
|
Input::PLACEHOLDER => $c = V::get(Vars::$VALUE),
|
||
|
|
Input::LABEL => $c,
|
||
|
|
],
|
||
|
|
],
|
||
|
|
]
|
||
|
|
],
|
||
|
|
self::getCommonFormSubmits($values),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
static function columnInfo() : array {
|
||
|
|
return [
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME =>&self::$ID,
|
||
|
|
C::TYPE =>eColumnType::INT,
|
||
|
|
C::AUTO_INCREMENT =>true,
|
||
|
|
C::PRIMARY =>true,
|
||
|
|
C::DEFAULT =>0,
|
||
|
|
C::HIDDEN =>true,
|
||
|
|
C::TH =>V::get(Vars::$ID),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$PROFILE,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => intval(PID),
|
||
|
|
C::TH => V::get(Vars::$PROFILE),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$ORDER,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::TH => V::get(Vars::$ORDER),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$VALUE,
|
||
|
|
C::TYPE => eColumnType::DOUBLE,
|
||
|
|
C::TH => V::get(Vars::$VALUE),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$ACCOUNT,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::LENGTH => 11,
|
||
|
|
C::TH => V::get(Vars::$ACCOUNT),
|
||
|
|
C::DEFAULT => intval(\Site::$owner_id),
|
||
|
|
c::HIDDEN=>true,
|
||
|
|
c::FUNC_VALUE => function($v){
|
||
|
|
if($id = $v[self::$ACCOUNT]) {
|
||
|
|
return \Account::getName( \Account::getByID($id) );
|
||
|
|
} else return '-';
|
||
|
|
},
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$CREATED,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::LENGTH => 11,
|
||
|
|
C::TH => V::get(Vars::$CREATED),
|
||
|
|
C::DEFAULT => time(),
|
||
|
|
c::FUNC_VALUE => function($v){return self::formatDate($v[self::$CREATED]);},
|
||
|
|
c::HIDDEN=>true,
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$UPDATED,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => time(),
|
||
|
|
C::TH => V::get(Vars::$UPDATED),
|
||
|
|
c::FUNC_VALUE => function($v){return self::formatDate($v[self::$UPDATED]);},
|
||
|
|
c::HIDDEN=>true,
|
||
|
|
]),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
new Cashback();
|
||
|
|
|
||
|
|
|
||
|
|
///
|