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.

207 lines
6.0KB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\db\ActiveRecord;
  5. use common\models\User;
  6. use common\models\Commande;
  7. use yii\helpers\Html;
  8. /**
  9. * This is the model class for table "credit_historique".
  10. *
  11. * @property integer $id
  12. * @property integer $id_user
  13. * @property integer $id_commande
  14. * @property string $date
  15. * @property double $montant
  16. * @property string $type
  17. * @property integer $id_etablissement
  18. * @property string $moyen_paiement
  19. */
  20. class CreditHistorique extends ActiveRecord {
  21. const TYPE_CREDIT_INITIAL = 'credit-initial';
  22. const TYPE_CREDIT = 'credit';
  23. const TYPE_PAIEMENT = 'paiement';
  24. const TYPE_REMBOURSEMENT = 'remboursement';
  25. const TYPE_DEBIT = 'debit';
  26. const MOYEN_CB = 'cb';
  27. const MOYEN_ESPECES = 'especes';
  28. const MOYEN_CHEQUE = 'cheque';
  29. const MOYEN_AUTRE = 'autre';
  30. /**
  31. * @inheritdoc
  32. */
  33. public static function tableName() {
  34. return 'credit_historique';
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules() {
  40. return [
  41. [['montant'], 'required'],
  42. [['id_user', 'id_user_action', 'id_commande', 'id_etablissement'], 'integer'],
  43. [['date'], 'safe'],
  44. [['montant'], 'double'],
  45. [['type', 'moyen_paiement', 'commentaire'], 'string', 'max' => 255],
  46. ];
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function attributeLabels() {
  52. return [
  53. 'id' => 'ID',
  54. 'id_user' => 'Utilisateur',
  55. 'id_user_action' => 'Utilisateur',
  56. 'id_commande' => 'Commande',
  57. 'date' => 'Date',
  58. 'montant' => 'Montant',
  59. 'type' => 'Type',
  60. 'id_etablissement' => 'Établissement',
  61. 'moyen_paiement' => 'Moyen de paiement',
  62. 'commentaire' => 'Commentaire',
  63. ];
  64. }
  65. public function getUser() {
  66. return $this->hasOne(User::className(), ['id' => 'id_user']);
  67. }
  68. public function getUserAction() {
  69. return $this->hasOne(User::className(), ['id' => 'id_user_action']);
  70. }
  71. public function getCommande() {
  72. return $this->hasOne(Commande::className(), ['id' => 'id_commande']);
  73. }
  74. public function getLibelleType() {
  75. switch ($this->type) {
  76. case 'paiement':
  77. return 'Paiement';
  78. break;
  79. case 'remboursement':
  80. return 'Remboursement';
  81. break;
  82. case 'debit':
  83. return 'Débit';
  84. break;
  85. }
  86. }
  87. public function save($runValidation = true, $attributeNames = NULL) {
  88. // initialisation du commentaire avant sauvegarde
  89. $this->commentaire = $this->getStrCommentaire() ;
  90. parent::save($runValidation, $attributeNames);
  91. // mise à jour du crédit au niveau de UserEtablissement
  92. $user_etablissement = UserEtablissement::findOne([
  93. 'id_user' => $this->id_user,
  94. 'id_etablissement' => $this->id_etablissement
  95. ]);
  96. if ($user_etablissement) {
  97. if ($this->isTypeCredit()) {
  98. $user_etablissement->credit += $this->montant;
  99. } elseif ($this->isTypeDebit()) {
  100. $user_etablissement->credit -= $this->montant;
  101. }
  102. $user_etablissement->save();
  103. }
  104. }
  105. public function isTypeDebit() {
  106. return in_array($this->type, [
  107. self::TYPE_DEBIT,
  108. self::TYPE_PAIEMENT,
  109. ]) ;
  110. }
  111. public function isTypeCredit() {
  112. return in_array($this->type, [
  113. self::TYPE_CREDIT,
  114. self::TYPE_CREDIT_INITIAL,
  115. self::TYPE_REMBOURSEMENT
  116. ]) ;
  117. }
  118. public function getMontant($format = false) {
  119. if($format)
  120. return number_format($this->montant,2) .' €' ;
  121. else
  122. return $this->montant ;
  123. }
  124. public function getStrLibelle() {
  125. $str = '' ;
  126. if($this->type == self::TYPE_CREDIT_INITIAL) {
  127. $str = 'Crédit initial' ;
  128. }
  129. elseif($this->type == self::TYPE_CREDIT) {
  130. $str = 'Crédit' ;
  131. }
  132. elseif($this->type == self::TYPE_PAIEMENT) {
  133. $str = 'Paiement' ;
  134. }
  135. elseif($this->type == self::TYPE_REMBOURSEMENT) {
  136. $str = 'Remboursement' ;
  137. }
  138. elseif($this->type == self::TYPE_DEBIT) {
  139. $str = 'Débit' ;
  140. }
  141. if($this->type == self::TYPE_PAIEMENT || $this->type == self::TYPE_REMBOURSEMENT) {
  142. if(isset($this->commande)) {
  143. $str .= '<br />Commande : '.date('d/m/Y',strtotime($this->commande->production->date)) ;
  144. }
  145. else {
  146. $str .= '<br />Commande supprimée' ;
  147. }
  148. }
  149. return $str ;
  150. }
  151. public function getStrCommentaire() {
  152. $str = $this->getStrLibelle() ;
  153. if(isset($this->commande)) {
  154. $str .= '<br />Montant de la commande : '.$this->commande->getMontant(true) ;
  155. }
  156. if(isset($this->user)) {
  157. $str .= '<br />Client : '.Html::encode($this->user->nom. ' '.$this->user->prenom) ;
  158. }
  159. if(isset($this->userAction)) {
  160. $str .= '<br />Action : '.Html::encode($this->userAction->nom. ' '.$this->userAction->prenom) ;
  161. }
  162. return $str ;
  163. }
  164. public function getDate($format = false) {
  165. if($format)
  166. return date('d/m/Y à H:i:s',strtotime($this->date)) ;
  167. else
  168. return $this->date ;
  169. }
  170. public function getStrMoyenPaiement() {
  171. switch($this->moyen_paiement) {
  172. case CreditHistorique::MOYEN_ESPECES : return 'Espèces' ;
  173. case CreditHistorique::MOYEN_CHEQUE : return 'Chèque' ;
  174. case CreditHistorique::MOYEN_CB : return 'Carte bancaire' ;
  175. case CreditHistorique::MOYEN_AUTRE : return 'Autre' ;
  176. default: return 'Crédit pain' ;
  177. }
  178. }
  179. }