|
|
@@ -54,7 +54,8 @@ use yii\helpers\Html; |
|
|
|
* @property string $code_postal |
|
|
|
* @property string $ville |
|
|
|
*/ |
|
|
|
class Etablissement extends \yii\db\ActiveRecord { |
|
|
|
class Etablissement extends \yii\db\ActiveRecord |
|
|
|
{ |
|
|
|
|
|
|
|
const PAIEMENT_OK = 'ok'; |
|
|
|
const PAIEMENT_ESSAI = 'essai'; |
|
|
@@ -64,14 +65,16 @@ class Etablissement extends \yii\db\ActiveRecord { |
|
|
|
/** |
|
|
|
* @inheritdoc |
|
|
|
*/ |
|
|
|
public static function tableName() { |
|
|
|
public static function tableName() |
|
|
|
{ |
|
|
|
return 'etablissement'; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @inheritdoc |
|
|
|
*/ |
|
|
|
public function rules() { |
|
|
|
public function rules() |
|
|
|
{ |
|
|
|
return [ |
|
|
|
[['nom', 'siret', 'heure_limite_commande', 'delai_commande','type'], 'required'], |
|
|
|
[['heure_limite_commande', 'delai_commande'], 'integer'], |
|
|
@@ -97,7 +100,8 @@ class Etablissement extends \yii\db\ActiveRecord { |
|
|
|
/** |
|
|
|
* @inheritdoc |
|
|
|
*/ |
|
|
|
public function attributeLabels() { |
|
|
|
public function attributeLabels() |
|
|
|
{ |
|
|
|
return [ |
|
|
|
'id' => 'ID', |
|
|
|
'nom' => 'Nom', |
|
|
@@ -120,20 +124,37 @@ class Etablissement extends \yii\db\ActiveRecord { |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
public function getUserEtablissement() { |
|
|
|
return $this->hasMany(UserEtablissement::className(), ['id_etablissement' => 'id']); |
|
|
|
/* |
|
|
|
* Relations |
|
|
|
*/ |
|
|
|
|
|
|
|
public function getUserEtablissement() |
|
|
|
{ |
|
|
|
return $this->hasMany( |
|
|
|
UserEtablissement::className(), |
|
|
|
['id_etablissement' => 'id'] |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
public function getUser() { |
|
|
|
public function getUser() |
|
|
|
{ |
|
|
|
return $this->hasMany(User::className(), ['id_etablissement' => 'id']); |
|
|
|
} |
|
|
|
|
|
|
|
public function getContact() { |
|
|
|
return $this->hasMany(User::className(), ['id_etablissement' => 'id'])->where(['status' => User::STATUS_BOULANGER]); |
|
|
|
public function getContact() |
|
|
|
{ |
|
|
|
return $this->hasMany(User::className(),['id_etablissement' => 'id']) |
|
|
|
->where(['status' => User::STATUS_BOULANGER]); |
|
|
|
} |
|
|
|
|
|
|
|
public static function getEtablissementsPopulateDropdown() { |
|
|
|
|
|
|
|
/** |
|
|
|
* Retourne la liste des établissements pour l'initialisation d'une liste |
|
|
|
* sélective. |
|
|
|
* |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
public static function getEtablissementsPopulateDropdown() |
|
|
|
{ |
|
|
|
$etablissements_dispos = Etablissement::find() |
|
|
|
->where(['actif' => 1]) |
|
|
|
->orderby('code_postal, ville ASC') |
|
|
@@ -159,42 +180,31 @@ class Etablissement extends \yii\db\ActiveRecord { |
|
|
|
return ['data' => $data_etablissements_dispos, 'options' => $options_etablissements_dispos]; |
|
|
|
} |
|
|
|
|
|
|
|
public function etatPaiement() { |
|
|
|
$date_limite = strtotime($this->date_creation) + 30 * 24 * 60 * 60; |
|
|
|
$date = time(); |
|
|
|
$date_paiement = strtotime($this->date_paiement); |
|
|
|
|
|
|
|
if ($date < $date_paiement + 30 * 24 * 60 * 60 || $this->gratuit) { |
|
|
|
return 'ok'; |
|
|
|
} else { |
|
|
|
if ($date < $date_limite) { |
|
|
|
return 'essai'; |
|
|
|
} else { |
|
|
|
if (!$this->date_paiement) |
|
|
|
return 'essai-terminee'; |
|
|
|
else |
|
|
|
return 'retard-paiement'; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function getCA($periode = '', $format = false) { |
|
|
|
/** |
|
|
|
* Retourne le CA de l'établissement pour un mois donné. |
|
|
|
* |
|
|
|
* @param string $periode |
|
|
|
* @param boolean $format |
|
|
|
* @return string |
|
|
|
*/ |
|
|
|
public function getCA($periode = '', $format = false) |
|
|
|
{ |
|
|
|
if (!$periode) |
|
|
|
$periode = date('Y-m'); |
|
|
|
|
|
|
|
$connection = Yii::$app->getDb(); |
|
|
|
$command = $connection->createCommand(' |
|
|
|
SELECT SUM(IF(produit.vrac,0,commande_produit.prix * commande_produit.quantite)) AS CA |
|
|
|
FROM commande, commande_produit, production, produit |
|
|
|
WHERE commande.id = commande_produit.id_commande |
|
|
|
AND production.id_etablissement = :id_etablissement |
|
|
|
AND commande.id_production = production.id |
|
|
|
AND commande_produit.id_produit = produit.id |
|
|
|
AND production.date > :date_debut |
|
|
|
AND production.date < :date_fin', [ |
|
|
|
':date_debut' => date('Y-m-31', strtotime("-1 month", strtotime($periode))), |
|
|
|
':date_fin' => date('Y-m-01', strtotime("+1 month", strtotime($periode))), |
|
|
|
':id_etablissement' => $this->id |
|
|
|
SELECT SUM(IF(produit.vrac,0,commande_produit.prix * commande_produit.quantite)) AS CA |
|
|
|
FROM commande, commande_produit, production, produit |
|
|
|
WHERE commande.id = commande_produit.id_commande |
|
|
|
AND production.id_etablissement = :id_etablissement |
|
|
|
AND commande.id_production = production.id |
|
|
|
AND commande_produit.id_produit = produit.id |
|
|
|
AND production.date > :date_debut |
|
|
|
AND production.date < :date_fin', [ |
|
|
|
':date_debut' => date('Y-m-31', strtotime("-1 month", strtotime($periode))), |
|
|
|
':date_fin' => date('Y-m-01', strtotime("+1 month", strtotime($periode))), |
|
|
|
':id_etablissement' => $this->id |
|
|
|
]); |
|
|
|
|
|
|
|
$result = $command->queryOne(); |
|
|
@@ -206,7 +216,16 @@ class Etablissement extends \yii\db\ActiveRecord { |
|
|
|
return $ca; |
|
|
|
} |
|
|
|
|
|
|
|
public function getMontantFacturer($periode = '', $ca = 0, $format = false) { |
|
|
|
/** |
|
|
|
* Retourne le montant à facturer pour une période donnée. |
|
|
|
* |
|
|
|
* @param string $periode |
|
|
|
* @param float $ca |
|
|
|
* @param boolean $format |
|
|
|
* @return string |
|
|
|
*/ |
|
|
|
public function getMontantFacturer($periode = '', $ca = 0, $format = false) |
|
|
|
{ |
|
|
|
if (!$periode) |
|
|
|
$periode = date('Y-m'); |
|
|
|
|
|
|
@@ -226,7 +245,14 @@ class Etablissement extends \yii\db\ActiveRecord { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function getFacture($periode = '') { |
|
|
|
/** |
|
|
|
* Retourne la facture d'une période donnée. |
|
|
|
* |
|
|
|
* @param string $periode |
|
|
|
* @return Facture |
|
|
|
*/ |
|
|
|
public function getFacture($periode = '') |
|
|
|
{ |
|
|
|
if (!$periode) |
|
|
|
$periode = date('Y-m', strtotime('-1 month')); |
|
|
|
|
|
|
@@ -242,15 +268,30 @@ class Etablissement extends \yii\db\ActiveRecord { |
|
|
|
return $facture; |
|
|
|
} |
|
|
|
|
|
|
|
public function factureMoisDernier() { |
|
|
|
/** |
|
|
|
* Retourne la facture du mois dernier. |
|
|
|
* |
|
|
|
* @return Facture |
|
|
|
*/ |
|
|
|
public function factureMoisDernier() |
|
|
|
{ |
|
|
|
return $this->getFacture(date('Y-m', strtotime('-1 month'))); |
|
|
|
} |
|
|
|
|
|
|
|
public static function getConfig($config = '', $id_etablissement = 0) { |
|
|
|
/** |
|
|
|
* Retourne une configuration d'un établissement donné. |
|
|
|
* |
|
|
|
* @param string $config |
|
|
|
* @param integer $id_etablissement |
|
|
|
* @return mixed |
|
|
|
*/ |
|
|
|
public static function getConfig($config = '', $id_etablissement = 0) |
|
|
|
{ |
|
|
|
if (strlen($config)) { |
|
|
|
if (!$id_etablissement) |
|
|
|
if (!$id_etablissement) { |
|
|
|
$id_etablissement = Yii::$app->user->identity->id_etablissement; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$etablissement = self::findOne($id_etablissement); |
|
|
|
if ($etablissement) { |
|
|
|
return $etablissement->$config; |
|
|
@@ -260,13 +301,34 @@ class Etablissement extends \yii\db\ActiveRecord { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
public function getPrixLibre() { |
|
|
|
/** |
|
|
|
* Retourne le montant de l'abonnement à prix libre définit par |
|
|
|
* l'établissement |
|
|
|
* |
|
|
|
* @param boolean $format |
|
|
|
* @return mixed |
|
|
|
*/ |
|
|
|
public function getPrixLibre($format = true) |
|
|
|
{ |
|
|
|
if (!is_null($this->prix_libre)) { |
|
|
|
return number_format($this->prix_libre, 2, ',', false) . ' € HT'; |
|
|
|
if($format) { |
|
|
|
return number_format($this->prix_libre, 2, ',', false) . ' € HT'; |
|
|
|
} |
|
|
|
else { |
|
|
|
return $this->prix_libre; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static function addUser($id_user, $id_producer) { |
|
|
|
/** |
|
|
|
* Lie un utilisateur à un producteur. |
|
|
|
* |
|
|
|
* @param integer $id_user |
|
|
|
* @param integer $id_producer |
|
|
|
* @return UserProducer |
|
|
|
*/ |
|
|
|
public static function addUser($id_user, $id_producer) |
|
|
|
{ |
|
|
|
$user_producer = UserEtablissement::find() |
|
|
|
->where([ |
|
|
|
'id_user' => $id_user, |