Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

202 lines
6.0KB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use common\models\Etablissement ;
  5. use common\models\PointVente ;
  6. use common\models\Commande ;
  7. use common\models\CommandeProduit ;
  8. /**
  9. * This is the model class for table "commande_auto".
  10. *
  11. * @property integer $id
  12. * @property integer $id_user
  13. * @property integer $id_etablissement
  14. * @property integer $id_point_vente
  15. * @property string $date_debut
  16. * @property string $date_fin
  17. * @property integer $lundi
  18. * @property integer $mardi
  19. * @property integer $mercredi
  20. * @property integer $jeudi
  21. * @property integer $vendredi
  22. * @property integer $samedi
  23. * @property integer $dimanche
  24. * @property integer $periodicite_semaine
  25. * @property string $username
  26. */
  27. class CommandeAuto extends \yii\db\ActiveRecord
  28. {
  29. /**
  30. * @inheritdoc
  31. */
  32. public static function tableName()
  33. {
  34. return 'commande_auto';
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['id_etablissement', 'id_point_vente'], 'required'],
  43. [['id_user', 'id_etablissement', 'id_point_vente', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche', 'periodicite_semaine'], 'integer'],
  44. [['date_debut', 'date_fin','username'], 'safe'],
  45. ];
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'id_user' => 'Utilisateur',
  55. 'id_etablissement' => 'Etablissement',
  56. 'id_point_vente' => 'Point de vente',
  57. 'date_debut' => 'Date de début',
  58. 'date_fin' => 'Date de fin',
  59. 'lundi' => 'Lundi',
  60. 'mardi' => 'Mardi',
  61. 'mercredi' => 'Mercredi',
  62. 'jeudi' => 'Jeudi',
  63. 'vendredi' => 'Vendredi',
  64. 'samedi' => 'Samedi',
  65. 'dimanche' => 'Dimanche',
  66. 'periodicite_semaine' => 'Périodicité',
  67. ];
  68. }
  69. public function getUser()
  70. {
  71. return $this->hasOne(User::className(), ['id'=>'id_user']) ;
  72. }
  73. public function getEtablissement()
  74. {
  75. return $this->hasOne(Etablissement::className(), ['id'=>'id_etablissement']) ;
  76. }
  77. public function getPointVente()
  78. {
  79. return $this->hasOne(PointVente::className(), ['id'=>'id_point_vente']) ;
  80. }
  81. public function getCommandeAutoProduit()
  82. {
  83. return $this->hasMany(CommandeAutoProduit::className(), ['id_commande_auto'=>'id'])->with('produit') ;
  84. }
  85. public static function getAll($date)
  86. {
  87. $date = date('Y-m-d', strtotime($date)) ;
  88. // commandes auto
  89. $commandes_auto = self::find()
  90. ->with('commandeAutoProduit')
  91. ->where(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
  92. ->all() ;
  93. $arr_commandes_auto = [] ;
  94. foreach($commandes_auto as $c)
  95. {
  96. // vérif dates
  97. if($date >= $c->date_debut &&
  98. (!$c->date_fin || $date <= $c->date_fin))
  99. {
  100. // périodicite
  101. $nb_jours = (strtotime($date) - strtotime($c->date_debut)) / (24*60*60) ;
  102. if($nb_jours % ($c->periodicite_semaine * 7) < 7 )
  103. {
  104. // jour de la semaine
  105. $jour = date('N', strtotime($date)) ;
  106. switch($jour)
  107. {
  108. case 1 : $jour = 'lundi' ; break ;
  109. case 2 : $jour = 'mardi' ; break ;
  110. case 3 : $jour = 'mercredi' ; break ;
  111. case 4 : $jour = 'jeudi' ; break ;
  112. case 5 : $jour = 'vendredi' ; break ;
  113. case 6 : $jour = 'samedi' ; break ;
  114. case 7 : $jour = 'dimanche' ; break ;
  115. }
  116. if($c->$jour)
  117. {
  118. $arr_commandes_auto[] = $c ;
  119. }
  120. }
  121. }
  122. }
  123. return $arr_commandes_auto ;
  124. }
  125. public static function addAll($date)
  126. {
  127. // production
  128. $production = Production::findOne(['date' => date('Y-m-d',strtotime($date))]) ;
  129. if($production)
  130. {
  131. $count_commandes_prod = Commande::find()
  132. ->where(['id_production' => $production->id])
  133. ->count() ;
  134. if(!$count_commandes_prod)
  135. {
  136. $commandes_auto = self::getAll($date) ;
  137. foreach($commandes_auto as $c)
  138. {
  139. $c->add($date) ;
  140. }
  141. }
  142. }
  143. }
  144. public function add($date)
  145. {
  146. // production
  147. $production = Production::findOne(['date' => date('Y-m-d',strtotime($date))]) ;
  148. if($production)
  149. {
  150. // commande
  151. $commande = new Commande ;
  152. if(strlen($this->username))
  153. {
  154. $commande->username = $this->username ;
  155. $commande->id_user = 0 ;
  156. }
  157. else {
  158. $commande->id_user = $this->id_user ;
  159. }
  160. $commande->date = date('Y-m-d H:i:s') ;
  161. $commande->type = Commande::TYPE_AUTO ;
  162. $commande->id_point_vente = $this->id_point_vente ;
  163. $commande->id_production = $production->id ;
  164. $commande->save() ;
  165. // produits
  166. foreach($this->commandeAutoProduit as $commande_auto_produit)
  167. {
  168. $commande_produit = new CommandeProduit ;
  169. $commande_produit->id_commande = $commande->id ;
  170. $commande_produit->id_produit = $commande_auto_produit->produit->id ;
  171. $commande_produit->quantite = $commande_auto_produit->quantite ;
  172. $commande_produit->prix = $commande_auto_produit->produit->prix ;
  173. $commande_produit->save() ;
  174. }
  175. }
  176. }
  177. }