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.

CreditHistorique.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 = '' ;
  153. if(strlen($this->commentaire)) {
  154. $str .= '<br />' ;
  155. }
  156. $str .= $this->getStrLibelle() ;
  157. if(isset($this->commande)) {
  158. $str .= '<br />Montant de la commande : '.$this->commande->getMontant(true) ;
  159. }
  160. if(isset($this->user)) {
  161. $str .= '<br />Client : '.Html::encode($this->user->nom. ' '.$this->user->prenom) ;
  162. }
  163. if(isset($this->userAction)) {
  164. $str .= '<br />Action : '.Html::encode($this->userAction->nom. ' '.$this->userAction->prenom) ;
  165. }
  166. return $str ;
  167. }
  168. public function getDate($format = false) {
  169. if($format)
  170. return date('d/m/Y à H:i:s',strtotime($this->date)) ;
  171. else
  172. return $this->date ;
  173. }
  174. public function getStrMoyenPaiement() {
  175. switch($this->moyen_paiement) {
  176. case CreditHistorique::MOYEN_ESPECES : return 'Espèces' ;
  177. case CreditHistorique::MOYEN_CHEQUE : return 'Chèque' ;
  178. case CreditHistorique::MOYEN_CB : return 'Carte bancaire' ;
  179. case CreditHistorique::MOYEN_AUTRE : return 'Autre' ;
  180. default: return 'Crédit pain' ;
  181. }
  182. }
  183. public function strUserAction() {
  184. if($this->userAction) {
  185. return $this->userAction->nom . ' ' . $this->userAction->prenom ;
  186. }
  187. else {
  188. return 'Système' ;
  189. }
  190. }
  191. }