Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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