您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

213 行
6.7KB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\helpers\Html;
  5. use common\models\PointVenteUser;
  6. use common\models\ProductionPointVente;
  7. /**
  8. * This is the model class for table "point_vente".
  9. *
  10. * @property integer $id
  11. * @property string $nom
  12. * @property string $adresse
  13. * @property integer $id_boulangerie
  14. */
  15. class PointVente extends \yii\db\ActiveRecord {
  16. var $commandes = [];
  17. var $recettes = 0;
  18. var $recettes_pain = 0;
  19. var $recettes_vrac = 0;
  20. var $data_select_commandes;
  21. var $data_options_commandes;
  22. var $users = [];
  23. var $users_commentaire = [];
  24. /**
  25. * @inheritdoc
  26. */
  27. public static function tableName() {
  28. return 'point_vente';
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules() {
  34. return [
  35. [['nom'], 'required'],
  36. [['acces_restreint'], 'boolean'],
  37. [['nom', 'code'], 'string', 'max' => 255],
  38. [['adresse', 'localite', 'horaires_lundi', 'horaires_mardi', 'horaires_mercredi', 'horaires_jeudi', 'horaires_vendredi', 'horaires_samedi', 'horaires_dimanche'], 'string'],
  39. [['point_fabrication', 'vrac', 'pain', 'credit_pain', 'livraison_lundi', 'livraison_mardi', 'livraison_mercredi', 'livraison_jeudi', 'livraison_vendredi', 'livraison_samedi', 'livraison_dimanche'], 'boolean'],
  40. ['point_fabrication', 'default', 'value' => 0],
  41. ['id_etablissement', 'integer'],
  42. ['id_etablissement', 'required'],
  43. [['users', 'users_commentaire', 'code'], 'safe']
  44. ];
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function attributeLabels() {
  50. return [
  51. 'id' => 'ID',
  52. 'nom' => 'Nom',
  53. 'adresse' => 'Adresse',
  54. 'localite' => 'Localité',
  55. 'point_fabrication' => 'Point de fabrication',
  56. 'horaires_lundi' => 'Lundi',
  57. 'horaires_mardi' => 'Mardi',
  58. 'horaires_mercredi' => 'Mercredi',
  59. 'horaires_jeudi' => 'Jeudi',
  60. 'horaires_vendredi' => 'Vendredi',
  61. 'horaires_samedi' => 'Samedi',
  62. 'horaires_dimanche' => 'Dimanche',
  63. 'vrac' => 'Livraison de vrac',
  64. 'pain' => 'Livraison de pain',
  65. 'acces_restreint' => 'Accès restreint',
  66. 'credit_pain' => 'Activer le Crédit Pain',
  67. 'livraison_lundi' => 'Lundi',
  68. 'livraison_mardi' => 'Mardi',
  69. 'livraison_mercredi' => 'Mercredi',
  70. 'livraison_jeudi' => 'Jeudi',
  71. 'livraison_vendredi' => 'Vendredi',
  72. 'livraison_samedi' => 'Samedi',
  73. 'livraison_dimanche' => 'Dimanche',
  74. 'code' => 'Code',
  75. ];
  76. }
  77. public function getPointVenteUser() {
  78. return $this->hasMany(PointVenteUser::className(), ['id_point_vente' => 'id']);
  79. }
  80. public function getProductionPointVente() {
  81. return $this->hasMany(ProductionPointVente::className(), ['id_point_vente' => 'id']);
  82. }
  83. public function initCommandes($commandes) {
  84. $this->commandes = [];
  85. $this->recettes = 0;
  86. $this->recettes_pain = 0;
  87. $this->recettes_vrac = 0;
  88. foreach ($commandes as $c) {
  89. if ($this->id == $c->id_point_vente) {
  90. $this->commandes[] = $c;
  91. $this->recettes += (float) $c->montant;
  92. $this->recettes_pain += (float) $c->montant_pain;
  93. $this->recettes_vrac += (float) $c->montant_vrac;
  94. }
  95. }
  96. }
  97. public function getCommandes() {
  98. return $this->commandes;
  99. }
  100. public function strListeVrac() {
  101. $str = '';
  102. $produits = Produit::find()->orderBy('order ASC')->all();
  103. foreach ($produits as $p) {
  104. if ($p->vrac) {
  105. $quantite = Commande::getQuantiteProduit($p->id, $this->commandes);
  106. if ($quantite) {
  107. $str .= $quantite . ' ' . Html::encode($p->diminutif) . ', ';
  108. }
  109. }
  110. }
  111. return substr($str, 0, strlen($str) - 2);
  112. }
  113. public function save($runValidation = true, $attributeNames = NULL) {
  114. $this->id_etablissement = Yii::$app->user->identity->id_etablissement;
  115. $this->pain = 1;
  116. return parent::save($runValidation, $attributeNames);
  117. }
  118. public function gestionPointFabrication() {
  119. if ($this->point_fabrication) {
  120. PointVente::updateAll(
  121. ['point_fabrication' => 0], ['id_etablissement' => $this->id_etablissement]
  122. );
  123. $this->point_fabrication = 1;
  124. $this->save();
  125. }
  126. }
  127. public function gestionAccesRestreint() {
  128. PointVenteUser::deleteAll(['id_point_vente' => $this->id]);
  129. if (is_array($this->users) && count($this->users)) {
  130. foreach ($this->users as $key => $val) {
  131. $user = User::findOne($val);
  132. if ($user) {
  133. $point_vente_user = new PointVenteUser;
  134. $point_vente_user->id_user = $val;
  135. $point_vente_user->id_point_vente = $this->id;
  136. if (isset($this->users_commentaire[$val]) && strlen($this->users_commentaire[$val]))
  137. $point_vente_user->commentaire = $this->users_commentaire[$val];
  138. $point_vente_user->save();
  139. }
  140. }
  141. }
  142. }
  143. public function getCommentaire() {
  144. if (isset($this->pointVenteUser)) {
  145. foreach ($this->pointVenteUser as $pvu) {
  146. if ($pvu->id_user == Yii::$app->user->identity->id) {
  147. return $pvu->commentaire;
  148. }
  149. }
  150. }
  151. }
  152. public static function count() {
  153. return PointVente::find()
  154. ->where([
  155. 'id_etablissement' => Yii::$app->user->identity->id_etablissement
  156. ])
  157. ->count();
  158. }
  159. public function verifCode($code) {
  160. if (strlen($this->code)) {
  161. if (trim(strtolower($code)) == trim(strtolower($this->code))) {
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. } else {
  167. return true;
  168. }
  169. }
  170. public function strJoursLivraison() {
  171. $str = '' ;
  172. if($this->livraison_lundi) $str .= 'lundi, ' ;
  173. if($this->livraison_mardi) $str .= 'mardi, ' ;
  174. if($this->livraison_mercredi) $str .= 'mercredi, ' ;
  175. if($this->livraison_jeudi) $str .= 'jeudi, ' ;
  176. if($this->livraison_vendredi) $str .= 'vendredi, ' ;
  177. if($this->livraison_samedi) $str .= 'samedi, ' ;
  178. if($this->livraison_dimanche) $str .= 'dimanche, ' ;
  179. if(strlen($str))
  180. return substr($str, 0, strlen($str)-2) ;
  181. else
  182. return '' ;
  183. }
  184. }