You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PointVente.php 7.2KB

8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if(is_null($c->date_delete)) {
  92. $this->recettes += (float) $c->montant;
  93. }
  94. $this->recettes_pain += (float) $c->montant_pain;
  95. $this->recettes_vrac += (float) $c->montant_vrac;
  96. }
  97. }
  98. }
  99. public function getCommandes() {
  100. return $this->commandes;
  101. }
  102. public function strListeVrac() {
  103. $str = '';
  104. $produits = Produit::find()->orderBy('order ASC')->all();
  105. foreach ($produits as $p) {
  106. if ($p->vrac) {
  107. $quantite = Commande::getQuantiteProduit($p->id, $this->commandes);
  108. if ($quantite) {
  109. $str .= $quantite . ' ' . Html::encode($p->diminutif) . ', ';
  110. }
  111. }
  112. }
  113. return substr($str, 0, strlen($str) - 2);
  114. }
  115. public function save($runValidation = true, $attributeNames = NULL) {
  116. $this->id_etablissement = Yii::$app->user->identity->id_etablissement;
  117. $this->pain = 1;
  118. return parent::save($runValidation, $attributeNames);
  119. }
  120. public function gestionPointFabrication() {
  121. if ($this->point_fabrication) {
  122. PointVente::updateAll(
  123. ['point_fabrication' => 0], ['id_etablissement' => $this->id_etablissement]
  124. );
  125. $this->point_fabrication = 1;
  126. $this->save();
  127. }
  128. }
  129. public function gestionAccesRestreint() {
  130. PointVenteUser::deleteAll(['id_point_vente' => $this->id]);
  131. if (is_array($this->users) && count($this->users)) {
  132. foreach ($this->users as $key => $val) {
  133. $user = User::findOne($val);
  134. if ($user) {
  135. $point_vente_user = new PointVenteUser;
  136. $point_vente_user->id_user = $val;
  137. $point_vente_user->id_point_vente = $this->id;
  138. if (isset($this->users_commentaire[$val]) && strlen($this->users_commentaire[$val]))
  139. $point_vente_user->commentaire = $this->users_commentaire[$val];
  140. $point_vente_user->save();
  141. }
  142. }
  143. }
  144. }
  145. public function getCommentaire() {
  146. if (isset($this->pointVenteUser)) {
  147. foreach ($this->pointVenteUser as $pvu) {
  148. if ($pvu->id_user == Yii::$app->user->identity->id) {
  149. return $pvu->commentaire;
  150. }
  151. }
  152. }
  153. }
  154. public static function count() {
  155. return PointVente::find()
  156. ->where([
  157. 'id_etablissement' => Yii::$app->user->identity->id_etablissement
  158. ])
  159. ->count();
  160. }
  161. public function verifCode($code) {
  162. if (strlen($this->code)) {
  163. if (trim(strtolower($code)) == trim(strtolower($this->code))) {
  164. return true;
  165. } else {
  166. return false;
  167. }
  168. } else {
  169. return true;
  170. }
  171. }
  172. public function strJoursLivraison() {
  173. $str = '' ;
  174. if($this->livraison_lundi) $str .= 'lundi, ' ;
  175. if($this->livraison_mardi) $str .= 'mardi, ' ;
  176. if($this->livraison_mercredi) $str .= 'mercredi, ' ;
  177. if($this->livraison_jeudi) $str .= 'jeudi, ' ;
  178. if($this->livraison_vendredi) $str .= 'vendredi, ' ;
  179. if($this->livraison_samedi) $str .= 'samedi, ' ;
  180. if($this->livraison_dimanche) $str .= 'dimanche, ' ;
  181. if(strlen($str))
  182. return substr($str, 0, strlen($str)-2) ;
  183. else
  184. return '' ;
  185. }
  186. public function strInfos($jour) {
  187. $str = '' ;
  188. $champs = 'horaires_'.$jour ;
  189. if(strlen($this->$champs)) {
  190. $str = nl2br(Html::encode($this->$champs)) ;
  191. $str = preg_replace('/\[select_previous_day\](.*?)\[\/select_previous_day\]/', '<a href="javascript:void(0);" class="select-previous-day">$1</a>' , $str) ;
  192. }
  193. return $str ;
  194. }
  195. }