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

386 lines
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. // payé
  191. if ($this->getMontant() - $this->getMontantPaye() < 0.01 &&
  192. $this->getMontant() - $this->getMontantPaye() >= 0) {
  193. return self::STATUT_PAYEE;
  194. }
  195. // à rembourser
  196. elseif ($this->getMontant() - $this->getMontantPaye() <= -0.01) {
  197. return self::STATUT_SURPLUS;
  198. }
  199. // reste à payer
  200. elseif ($this->getMontant() - $this->getMontantPaye() >= 0.01) {
  201. return self::STATUT_IMPAYEE;
  202. }
  203. }
  204. public function getResumePanier() {
  205. if (!isset($this->commandeProduits))
  206. $this->commandeProduits = CommandeProduit::find()->where(['id_commande' => $this->id])->all();
  207. $html = '';
  208. $count = count($this->commandeProduits);
  209. $i = 0;
  210. foreach ($this->commandeProduits as $p) {
  211. if (isset($p->produit)) {
  212. $html .= $p->quantite . ' x ' . Html::encode($p->produit->nom);
  213. if (++$i != $count)
  214. $html .= '<br />';
  215. }
  216. }
  217. return $html;
  218. }
  219. public function getResumePointVente() {
  220. $html = '';
  221. if (isset($this->pointVente)) {
  222. $html .= '<span class="nom-point-vente">' . Html::encode($this->pointVente->nom) . '</span>'
  223. . '<br /><span class="localite">' . Html::encode($this->pointVente->localite) . '</span>';
  224. if (strlen($this->commentaire_point_vente)) {
  225. $html .= '<div class="commentaire"><span>' . Html::encode($this->commentaire_point_vente) . '</span></div>';
  226. }
  227. } else {
  228. $html .= 'Point de vente supprimé';
  229. }
  230. return $html;
  231. }
  232. public function getStrMontant() {
  233. return number_format($this->montant, 2) . ' €';
  234. }
  235. public function getResumeMontant() {
  236. $html = '';
  237. $html .= $this->getStrMontant() . '<br />';
  238. if ($this->montant_paye) {
  239. if ($this->getStatutPaiement() == Commande::STATUT_PAYEE) {
  240. $html .= '<span class="label label-success">Payée</span>';
  241. } elseif ($this->getStatutPaiement() == Commande::STATUT_IMPAYEE) {
  242. $html .= '<span class="label label-danger">Non payée</span><br />
  243. Reste <strong>' . $this->getMontantRestant(true) . '</strong> à payer';
  244. } elseif ($this->getStatutPaiement() == Commande::STATUT_SURPLUS) {
  245. $html .= '<span class="label label-success">Payée</span>';
  246. }
  247. } else {
  248. $html .= '<span class="label label-default">À régler sur place</span>';
  249. }
  250. return $html;
  251. }
  252. public function getStrUser() {
  253. if (isset($this->user)) {
  254. return Html::encode($this->user->prenom . ' ' . $this->user->nom);
  255. } elseif (strlen($this->username)) {
  256. return Html::encode($this->username);
  257. } else {
  258. return 'Client introuvable';
  259. }
  260. }
  261. public static function findBy($params = []) {
  262. if (!isset($params['id_etablissement']))
  263. $params['id_etablissement'] = Yii::$app->user->identity->id_etablissement;
  264. $commandes = Commande::find()
  265. ->with('commandeProduits', 'creditHistorique', 'pointVente')
  266. ->joinWith(['production', 'user','user.userEtablissement'])
  267. ->where(['production.id_etablissement' => $params['id_etablissement']]);
  268. if (isset($params['condition']))
  269. $commandes = $commandes->andWhere($params['condition']);
  270. if (isset($params['date']))
  271. $commandes = $commandes->andWhere(['production.date' => $params['date']]);
  272. if (isset($params['type']))
  273. $commandes = $commandes->andWhere(['commande.type' => $params['type']]);
  274. if (isset($params['orderby']))
  275. $commandes = $commandes->orderBy($params['orderby']);
  276. else
  277. $commandes = $commandes->orderBy('date ASC');
  278. if (isset($params['limit']))
  279. $commandes = $commandes->limit($params['limit']);
  280. $commandes = $commandes->all();
  281. return $commandes;
  282. }
  283. public function getEtat() {
  284. $delai_commande = Etablissement::getConfig('delai_commande', $this->production->id_etablissement);
  285. $heure_limite = Etablissement::getConfig('heure_limite_commande', $this->production->id_etablissement);
  286. $date_commande = strtotime($this->production->date);
  287. $date_today = strtotime(date('Y-m-d'));
  288. $heure_today = date('G');
  289. $nb_jours = (int) (($date_commande - $date_today) / (24 * 60 * 60));
  290. if ($nb_jours <= 0) {
  291. return self::ETAT_LIVREE;
  292. } elseif ($nb_jours >= $delai_commande &&
  293. ($nb_jours != $delai_commande ||
  294. ($nb_jours == $delai_commande && $heure_today < $heure_limite))) {
  295. return self::ETAT_MODIFIABLE;
  296. }
  297. return self::ETAT_PREPARATION;
  298. }
  299. public function getStrType($with_label = false) {
  300. $class_label = '';
  301. $str = '';
  302. if ($this->type == self::TYPE_USER) {
  303. $class_label = 'success';
  304. $str = 'Client';
  305. } elseif ($this->type == self::TYPE_AUTO) {
  306. $class_label = 'default';
  307. $str = 'Auto';
  308. } elseif ($this->type == self::TYPE_ADMIN) {
  309. $class_label = 'warning';
  310. $str = 'Vous';
  311. }
  312. if ($with_label)
  313. return '<span class="label label-' . $class_label . '">' . $str . '</span>';
  314. else
  315. return $str;
  316. }
  317. }