|
- <?php
-
-
-
- namespace common\models;
-
- use common\helpers\GlobalParam;
- use Yii;
- use yii\helpers\Html;
- use common\models\UserPointSale;
- use common\models\PointSaleDistribution;
- use common\components\ActiveRecordCommon;
-
-
- class PointSale extends ActiveRecordCommon
- {
- var $orders = [];
- var $revenues = 0;
- var $revenues_with_tax = 0;
- var $data_select_orders;
- var $data_options_orders;
- var $users = [];
- var $users_comment = [];
-
-
-
- public static function tableName()
- {
- return 'point_sale';
- }
-
-
-
- public function rules()
- {
- return [
- [['name'], 'required'],
- [['restricted_access'], 'boolean'],
- [['name', 'code'], 'string', 'max' => 255],
- [['address', 'locality', 'infos_monday', 'infos_tuesday',
- 'infos_wednesday', 'infos_thursday', 'infos_friday',
- 'infos_saturday', 'infos_sunday', 'credit_functioning'], 'string'],
- [['point_production', 'credit', 'delivery_monday', 'delivery_tuesday',
- 'delivery_wednesday', 'delivery_thursday', 'delivery_friday',
- 'delivery_saturday', 'delivery_sunday', 'default'], 'boolean'],
- ['point_production', 'default', 'value' => 0],
- [['id_producer', 'id_user'], 'integer'],
- ['id_producer', 'required'],
- [['users', 'users_comment', 'code'], 'safe']
- ];
- }
-
-
-
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'name' => 'Nom',
- 'address' => 'Adresse',
- 'locality' => 'Localité',
- 'point_production' => 'Point de production',
- 'infos_monday' => 'Lundi',
- 'infos_tuesday' => 'Mardi',
- 'infos_wednesday' => 'Mercredi',
- 'infos_thursday' => 'Jeudi',
- 'infos_friday' => 'Vendredi',
- 'infos_saturday' => 'Samedi',
- 'infos_sunday' => 'Dimanche',
- 'restricted_access' => 'Accès restreint',
- 'credit' => 'Activer le Crédit',
- 'delivery_monday' => 'Lundi',
- 'delivery_tuesday' => 'Mardi',
- 'delivery_wednesday' => 'Mercredi',
- 'delivery_thursday' => 'Jeudi',
- 'delivery_friday' => 'Vendredi',
- 'delivery_saturday' => 'Samedi',
- 'delivery_sunday' => 'Dimanche',
- 'code' => 'Code',
- 'credit_functioning' => 'Utilisation du Crédit par l\'utilisateur',
- 'default' => 'Point de vente par défaut',
- 'id_user' => 'Contact'
- ];
- }
-
-
-
-
- public function getUserPointSale()
- {
- return $this->hasMany(
- UserPointSale::className(),
- ['id_point_sale' => 'id']
- );
- }
-
- public function getPointSaleDistribution()
- {
- return $this->hasMany(
- PointSaleDistribution::className(),
- ['id_point_sale' => 'id']
- );
- }
-
- public function getUser()
- {
- return $this->hasOne(
- User::className(),
- ['id' => 'id_user']
- ) ;
- }
-
-
-
- public static function defaultOptionsSearch()
- {
- return [
- 'with' => [],
- 'join_with' => [],
- 'orderby' => '',
- 'attribute_id_producer' => 'point_sale.id_producer'
- ];
- }
-
-
-
- public function initOrders($ordersArray)
- {
- $this->orders = [];
- $this->revenues = 0;
- $this->revenues_with_tax = 0;
-
- if ($ordersArray) {
- foreach ($ordersArray as $order) {
- if ($this->id == $order->id_point_sale) {
- $this->orders[] = $order;
-
- if (is_null($order->date_delete)) {
- $this->revenues += (float)$order->amount;
- $this->revenues_with_tax += (float)$order->amount_with_tax;
- }
- }
- }
- }
- }
-
-
-
- public function getOrders()
- {
- return $this->orders;
- }
-
-
-
- public function save($runValidation = true, $attributeNames = NULL)
- {
- $this->id_producer = GlobalParam::getCurrentProducerId();
- return parent::save($runValidation, $attributeNames);
- }
-
-
-
- public function processPointProduction()
- {
- if ($this->point_production) {
- PointSale::updateAll(
- ['point_production' => 0],
- ['id_producer' => $this->id_producer]
- );
- $this->point_production = 1;
- $this->save();
- }
- }
-
-
-
- public function processRestrictedAccess()
- {
- UserPointSale::deleteAll(['id_point_sale' => $this->id]);
-
- if (is_array($this->users) && count($this->users)) {
- foreach ($this->users as $key => $val) {
- $user = User::findOne($val);
- if ($user) {
- $userPointSale = new UserPointSale;
- $userPointSale->id_user = $val;
- $userPointSale->id_point_sale = $this->id;
- if (isset($this->users_comment[$val]) && strlen($this->users_comment[$val])) {
- $userPointSale->comment = $this->users_comment[$val];
- }
- $userPointSale->save();
- }
- }
- }
- }
-
-
-
- public function getComment()
- {
- if (isset($this->userPointSale)) {
- foreach ($this->userPointSale as $userPointSale) {
- if ($userPointSale->id_user == User::getCurrentId()) {
- return $userPointSale->comment;
- }
- }
- }
- return null;
- }
-
-
-
- public static function count()
- {
- return self::searchCount(['id_producer' => GlobalParam::getCurrentProducerId()]);
- }
-
-
-
- public function validateCode($code)
- {
- if (strlen($this->code)) {
- if (trim(strtolower($code)) == trim(strtolower($this->code))) {
- return true;
- } else {
- return false;
- }
- }
-
- return true;
- }
-
-
-
- public function getStrDeliveryDays()
- {
- $str = '';
-
- if ($this->delivery_monday) $str .= 'lundi, ';
- if ($this->delivery_tuesday) $str .= 'mardi, ';
- if ($this->delivery_wednesday) $str .= 'mercredi, ';
- if ($this->delivery_thursday) $str .= 'jeudi, ';
- if ($this->delivery_friday) $str .= 'vendredi, ';
- if ($this->delivery_saturday) $str .= 'samedi, ';
- if ($this->delivery_sunday) $str .= 'dimanche, ';
-
- if (strlen($str)) {
- return substr($str, 0, strlen($str) - 2);
- } else {
- return '';
- }
- }
-
-
-
- public function getStrInfos($day)
- {
- $str = '';
- $field = 'infos_' . $day;
-
- if (strlen($this->$field)) {
- $str = nl2br(Html::encode($this->$field));
- $str = preg_replace('/\[select_previous_day\](.*?)\[\/select_previous_day\]/', '<a href="javascript:void(0);" class="select-previous-day">$1</a>', $str);
- }
- return $str;
- }
-
-
-
- public function getCreditFunctioning()
- {
- return strlen($this->credit_functioning) > 0 ?
- $this->credit_functioning :
- Producer::getConfig('credit_functioning');
- }
-
-
-
- public function linkUser($idUser)
- {
- if ($idUser) {
- $userPointSale = UserPointSale::find()
- ->where([
- 'id_user' => $idUser,
- 'id_point_sale' => $this->id
- ])->one();
-
- if (!$userPointSale) {
- $userPointSale = new UserPointSale;
- $userPointSale->id_user = $idUser;
- $userPointSale->id_point_sale = $this->id;
- $userPointSale->save();
- }
- }
- }
- }
|