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ů.

216 lines
7.2KB

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