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.

385 line
12KB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\helpers\Html;
  5. use common\models\Etablissement;
  6. /**
  7. * This is the model class for table "commande".
  8. *
  9. * @property integer $id
  10. * @property integer $id_user
  11. * @property string $date
  12. * @property string $date_update
  13. * @property integer $id_point_vente
  14. * @property integer $id_production
  15. * @property boolean $paiement_automatique
  16. */
  17. class Commande extends \yii\db\ActiveRecord {
  18. var $montant = 0;
  19. var $montant_pain = 0;
  20. var $montant_vrac = 0;
  21. var $montant_paye = 0;
  22. var $poids_pain = 0;
  23. var $poids_vrac = 0;
  24. const TYPE_AUTO = 'auto';
  25. const TYPE_USER = 'user';
  26. const TYPE_ADMIN = 'admin';
  27. const STATUT_PAYEE = 'payee';
  28. const STATUT_IMPAYEE = 'impayee';
  29. const STATUT_SURPLUS = 'surplus';
  30. const ETAT_MODIFIABLE = 'ouverte';
  31. const ETAT_PREPARATION = 'preparation';
  32. const ETAT_LIVREE = 'livree';
  33. /**
  34. * @inheritdoc
  35. */
  36. public static function tableName() {
  37. return 'commande';
  38. }
  39. public function init() {
  40. if (isset($this->commandeProduits)) {
  41. foreach ($this->commandeProduits as $p) {
  42. if ($p->mode_vente == 'unite') {
  43. $this->montant_pain += $p->prix * $p->quantite;
  44. } elseif ($p->mode_vente == 'poids') {
  45. $this->montant_pain += $p->prix * $p->quantite / 1000;
  46. }
  47. }
  48. $this->montant = $this->montant_vrac + $this->montant_pain;
  49. }
  50. if (isset($this->creditHistorique) && !$this->montant_paye) {
  51. foreach ($this->creditHistorique as $ch) {
  52. if ($ch->type == CreditHistorique::TYPE_PAIEMENT)
  53. $this->montant_paye += $ch->montant;
  54. elseif ($ch->type == CreditHistorique::TYPE_REMBOURSEMENT)
  55. $this->montant_paye -= $ch->montant;
  56. }
  57. }
  58. }
  59. public static function getQuantiteProduit($id_produit, $commandes) {
  60. $quantite = 0;
  61. if (isset($commandes) && is_array($commandes) && count($commandes)) {
  62. foreach ($commandes as $c) {
  63. foreach ($c->commandeProduits as $cp) {
  64. if ($cp->id_produit == $id_produit)
  65. $quantite += $cp->quantite;
  66. }
  67. }
  68. }
  69. return $quantite;
  70. }
  71. /*
  72. * relations
  73. */
  74. public function getUser() {
  75. return $this->hasOne(User::className(), ['id' => 'id_user']);
  76. }
  77. public function getCommandeProduits() {
  78. return $this->hasMany(CommandeProduit::className(), ['id_commande' => 'id'])->with('produit');
  79. }
  80. public function getProduction() {
  81. return $this->hasOne(Production::className(), ['id' => 'id_production'])->with('etablissement');
  82. }
  83. public function getPointVente() {
  84. return $this->hasOne(PointVente::className(), ['id' => 'id_point_vente'])->with('pointVenteUser');
  85. }
  86. public function getCreditHistorique() {
  87. return $this->hasMany(CreditHistorique::className(), ['id_commande' => 'id']);
  88. }
  89. /**
  90. * @inheritdoc
  91. */
  92. public function rules() {
  93. return [
  94. [['id_user', 'date', 'id_point_vente', 'id_production'], 'required', 'message' => ''],
  95. [['id_user', 'id_point_vente', 'id_production'], 'integer'],
  96. [['paiement_automatique'], 'boolean'],
  97. [['date', 'date_update', 'commentaire', 'commentaire_point_vente'], 'safe']
  98. ];
  99. }
  100. /**
  101. * @inheritdoc
  102. */
  103. public function attributeLabels() {
  104. return [
  105. 'id' => 'ID',
  106. 'id_user' => 'Id User',
  107. 'date' => 'Date',
  108. 'date_update' => 'Date Update',
  109. 'id_point_vente' => 'Point de vente',
  110. 'id_production' => 'Date de production',
  111. ];
  112. }
  113. public function strListeVrac() {
  114. $str = '';
  115. foreach ($this->commandeProduits as $cp) {
  116. if ($cp->produit->vrac) {
  117. $str .= $cp->quantite . '&nbsp;' . Html::encode($cp->produit->diminutif) . ', ';
  118. }
  119. }
  120. return substr($str, 0, strlen($str) - 2);
  121. }
  122. public function getMontantPaye() {
  123. if ($this->montant_paye) {
  124. return $this->montant_paye;
  125. } else {
  126. $historique = CreditHistorique::find()
  127. ->where(['id_commande' => $this->id])
  128. ->all();
  129. $montant = 0;
  130. foreach ($historique as $ch) {
  131. if ($ch->type == CreditHistorique::TYPE_PAIEMENT)
  132. $montant += $ch->montant;
  133. elseif ($ch->type == CreditHistorique::TYPE_REMBOURSEMENT)
  134. $montant -= $ch->montant;
  135. }
  136. return $montant;
  137. }
  138. }
  139. public function getMontant($format = false) {
  140. if ($format)
  141. return number_format($this->getMontant(), 2) . ' €';
  142. else
  143. return $this->montant;
  144. }
  145. public function getMontantFormat() {
  146. return number_format($this->getMontant(), 2) . ' €';
  147. }
  148. public function getMontantRestant($format = false) {
  149. $montant_restant = $this->getMontant() - $this->getMontantPaye();
  150. if ($format)
  151. return number_format($montant_restant, 2) . ' €';
  152. else
  153. return $montant_restant;
  154. }
  155. public function getMontantSurplus($format = false) {
  156. $montant_surplus = $this->getMontantPaye() - $this->getMontant();
  157. if ($format)
  158. return number_format($montant_surplus, 2) . ' €';
  159. else
  160. return $montant_surplus;
  161. }
  162. public function getDataJson() {
  163. $commande = Commande::find()->with('commandeProduits')->where(['id' => $this->id])->one();
  164. $commande->init();
  165. $json_commande = [
  166. 'produits' => [],
  167. 'montant' => $commande->montant,
  168. 'str_montant' => $commande->getMontantFormat(),
  169. 'montant_paye' => $commande->getMontantPaye(),
  170. 'commentaire' => $commande->commentaire,
  171. ];
  172. foreach ($commande->commandeProduits as $commande_produit) {
  173. $json_commande['produits'][$commande_produit->id_produit] = $commande_produit->quantite;
  174. }
  175. return json_encode($json_commande);
  176. }
  177. public function creditHistorique($type, $montant, $id_etablissement, $id_user, $id_user_action) {
  178. $credit_historique = new CreditHistorique;
  179. $credit_historique->id_user = $this->id_user;
  180. $credit_historique->id_commande = $this->id;
  181. $credit_historique->montant = $montant;
  182. $credit_historique->type = $type;
  183. $credit_historique->id_etablissement = $id_etablissement;
  184. $credit_historique->id_user_action = $id_user_action;
  185. $credit_historique->populateRelation('commande', $this) ;
  186. $credit_historique->populateRelation('user', User::find()->where(['id' => $this->id_user])->one()) ;
  187. $credit_historique->save();
  188. }
  189. public function getStatutPaiement() {
  190. // à rembourser
  191. if ($this->getMontant() - $this->getMontantPaye() < 0) {
  192. return self::STATUT_SURPLUS;
  193. }
  194. // payé
  195. elseif ($this->getMontant() - $this->getMontantPaye() < 0.001) {
  196. return self::STATUT_PAYEE;
  197. }
  198. // reste à payer
  199. elseif ($this->getMontant() - $this->getMontantPaye() > 0.01) {
  200. return self::STATUT_IMPAYEE;
  201. }
  202. }
  203. public function getResumePanier() {
  204. if (!isset($this->commandeProduits))
  205. $this->commandeProduits = CommandeProduit::find()->where(['id_commande' => $this->id])->all();
  206. $html = '';
  207. $count = count($this->commandeProduits);
  208. $i = 0;
  209. foreach ($this->commandeProduits as $p) {
  210. if (isset($p->produit)) {
  211. $html .= $p->quantite . ' x ' . Html::encode($p->produit->nom);
  212. if (++$i != $count)
  213. $html .= '<br />';
  214. }
  215. }
  216. return $html;
  217. }
  218. public function getResumePointVente() {
  219. $html = '';
  220. if (isset($this->pointVente)) {
  221. $html .= '<span class="nom-point-vente">' . Html::encode($this->pointVente->nom) . '</span>'
  222. . '<br /><span class="localite">' . Html::encode($this->pointVente->localite) . '</span>';
  223. if (strlen($this->commentaire_point_vente)) {
  224. $html .= '<div class="commentaire"><span>' . Html::encode($this->commentaire_point_vente) . '</span></div>';
  225. }
  226. } else {
  227. $html .= 'Point de vente supprimé';
  228. }
  229. return $html;
  230. }
  231. public function getStrMontant() {
  232. return number_format($this->montant, 2) . ' €';
  233. }
  234. public function getResumeMontant() {
  235. $html = '';
  236. $html .= $this->getStrMontant() . '<br />';
  237. if ($this->montant_paye) {
  238. if ($this->getStatutPaiement() == Commande::STATUT_PAYEE) {
  239. $html .= '<span class="label label-success">Payée</span>';
  240. } elseif ($this->getStatutPaiement() == Commande::STATUT_IMPAYEE) {
  241. $html .= '<span class="label label-danger">Non payée</span><br />
  242. Reste <strong>' . $this->getMontantRestant(true) . '</strong> à payer';
  243. } elseif ($this->getStatutPaiement() == Commande::STATUT_SURPLUS) {
  244. $html .= '<span class="label label-success">Payée</span>';
  245. }
  246. } else {
  247. $html .= '<span class="label label-default">À régler sur place</span>';
  248. }
  249. return $html;
  250. }
  251. public function getStrUser() {
  252. if (isset($this->user)) {
  253. return Html::encode($this->user->prenom . ' ' . $this->user->nom);
  254. } elseif (strlen($this->username)) {
  255. return Html::encode($this->username);
  256. } else {
  257. return 'Client introuvable';
  258. }
  259. }
  260. public static function findBy($params = []) {
  261. if (!isset($params['id_etablissement']))
  262. $params['id_etablissement'] = Yii::$app->user->identity->id_etablissement;
  263. $commandes = Commande::find()
  264. ->with('commandeProduits', 'creditHistorique', 'pointVente')
  265. ->joinWith(['production', 'user','user.userEtablissement'])
  266. ->where(['production.id_etablissement' => $params['id_etablissement']]);
  267. if (isset($params['condition']))
  268. $commandes = $commandes->andWhere($params['condition']);
  269. if (isset($params['date']))
  270. $commandes = $commandes->andWhere(['production.date' => $params['date']]);
  271. if (isset($params['type']))
  272. $commandes = $commandes->andWhere(['commande.type' => $params['type']]);
  273. if (isset($params['orderby']))
  274. $commandes = $commandes->orderBy($params['orderby']);
  275. else
  276. $commandes = $commandes->orderBy('date ASC');
  277. if (isset($params['limit']))
  278. $commandes = $commandes->limit($params['limit']);
  279. $commandes = $commandes->all();
  280. return $commandes;
  281. }
  282. public function getEtat() {
  283. $delai_commande = Etablissement::getConfig('delai_commande', $this->production->id_etablissement);
  284. $heure_limite = Etablissement::getConfig('heure_limite_commande', $this->production->id_etablissement);
  285. $date_commande = strtotime($this->production->date);
  286. $date_today = strtotime(date('Y-m-d'));
  287. $heure_today = date('G');
  288. $nb_jours = (int) (($date_commande - $date_today) / (24 * 60 * 60));
  289. if ($nb_jours <= 0) {
  290. return self::ETAT_LIVREE;
  291. } elseif ($nb_jours >= $delai_commande &&
  292. ($nb_jours != $delai_commande ||
  293. ($nb_jours == $delai_commande && $heure_today < $heure_limite))) {
  294. return self::ETAT_MODIFIABLE;
  295. }
  296. return self::ETAT_PREPARATION;
  297. }
  298. public function getStrType($with_label = false) {
  299. $class_label = '';
  300. $str = '';
  301. if ($this->type == self::TYPE_USER) {
  302. $class_label = 'success';
  303. $str = 'Client';
  304. } elseif ($this->type == self::TYPE_AUTO) {
  305. $class_label = 'default';
  306. $str = 'Auto';
  307. } elseif ($this->type == self::TYPE_ADMIN) {
  308. $class_label = 'warning';
  309. $str = 'Vous';
  310. }
  311. if ($with_label)
  312. return '<span class="label label-' . $class_label . '">' . $str . '</span>';
  313. else
  314. return $str;
  315. }
  316. }