commandeProduits)) {
foreach ($this->commandeProduits as $p) {
if ($p->mode_vente == 'unite') {
$this->montant_pain += $p->prix * $p->quantite;
} elseif ($p->mode_vente == 'poids') {
$this->montant_pain += $p->prix * $p->quantite / 1000;
}
}
$this->montant = $this->montant_vrac + $this->montant_pain;
}
if (isset($this->creditHistorique) && !$this->montant_paye) {
foreach ($this->creditHistorique as $ch) {
if ($ch->type == CreditHistorique::TYPE_PAIEMENT)
$this->montant_paye += $ch->montant;
elseif ($ch->type == CreditHistorique::TYPE_REMBOURSEMENT)
$this->montant_paye -= $ch->montant;
}
}
}
public static function getQuantiteProduit($id_produit, $commandes) {
$quantite = 0;
if (isset($commandes) && is_array($commandes) && count($commandes)) {
foreach ($commandes as $c) {
foreach ($c->commandeProduits as $cp) {
if ($cp->id_produit == $id_produit)
$quantite += $cp->quantite;
}
}
}
return $quantite;
}
/*
* relations
*/
public function getUser() {
return $this->hasOne(User::className(), ['id' => 'id_user']);
}
public function getCommandeProduits() {
return $this->hasMany(CommandeProduit::className(), ['id_commande' => 'id'])->with('produit');
}
public function getProduction() {
return $this->hasOne(Production::className(), ['id' => 'id_production'])->with('etablissement');
}
public function getPointVente() {
return $this->hasOne(PointVente::className(), ['id' => 'id_point_vente'])->with('pointVenteUser');
}
public function getCreditHistorique() {
return $this->hasMany(CreditHistorique::className(), ['id_commande' => 'id']);
}
/**
* @inheritdoc
*/
public function rules() {
return [
[['id_user', 'date', 'id_point_vente', 'id_production'], 'required', 'message' => ''],
[['id_user', 'id_point_vente', 'id_production'], 'integer'],
[['date', 'date_update', 'commentaire', 'commentaire_point_vente'], 'safe']
];
}
/**
* @inheritdoc
*/
public function attributeLabels() {
return [
'id' => 'ID',
'id_user' => 'Id User',
'date' => 'Date',
'date_update' => 'Date Update',
'id_point_vente' => 'Point de vente',
'id_production' => 'Date de production',
];
}
public function strListeVrac() {
$str = '';
foreach ($this->commandeProduits as $cp) {
if ($cp->produit->vrac) {
$str .= $cp->quantite . ' ' . Html::encode($cp->produit->diminutif) . ', ';
}
}
return substr($str, 0, strlen($str) - 2);
}
public function getMontantPaye() {
if ($this->montant_paye) {
return $this->montant_paye;
} else {
$historique = CreditHistorique::find()
->where(['id_commande' => $this->id])
->all();
$montant = 0;
foreach ($historique as $ch) {
if ($ch->type == CreditHistorique::TYPE_PAIEMENT)
$montant += $ch->montant;
elseif ($ch->type == CreditHistorique::TYPE_REMBOURSEMENT)
$montant -= $ch->montant;
}
return $montant;
}
}
public function getMontant($format = false) {
if ($format)
return number_format($this->getMontant(), 2) . ' €';
else
return $this->montant;
}
public function getMontantFormat() {
return number_format($this->getMontant(), 2) . ' €';
}
public function getMontantRestant($format = false) {
$montant_restant = $this->getMontant() - $this->getMontantPaye();
if ($format)
return number_format($montant_restant, 2) . ' €';
else
return $montant_restant;
}
public function getMontantSurplus($format = false) {
$montant_surplus = $this->getMontantPaye() - $this->getMontant();
if ($format)
return number_format($montant_surplus, 2) . ' €';
else
return $montant_surplus;
}
public function getDataJson() {
$commande = Commande::find()->with('commandeProduits')->where(['id' => $this->id])->one();
$commande->init();
$json_commande = [
'produits' => [],
'montant' => $commande->montant,
'str_montant' => $commande->getMontantFormat(),
'montant_paye' => $commande->getMontantPaye(),
'commentaire' => $commande->commentaire,
];
foreach ($commande->commandeProduits as $commande_produit) {
$json_commande['produits'][$commande_produit->id_produit] = $commande_produit->quantite;
}
return json_encode($json_commande);
}
public function creditHistorique($type, $montant, $id_etablissement, $id_user, $id_user_action) {
$credit_historique = new CreditHistorique;
$credit_historique->id_user = $this->id_user;
$credit_historique->id_commande = $this->id;
$credit_historique->montant = $montant;
$credit_historique->type = $type;
$credit_historique->id_etablissement = $id_etablissement;
$credit_historique->id_user_action = $id_user_action;
$credit_historique->populateRelation('commande', $this) ;
$credit_historique->populateRelation('user', User::find()->where(['id' => $this->id_user])->one()) ;
$credit_historique->save();
}
public function getStatutPaiement() {
// à rembourser
if ($this->getMontant() - $this->getMontantPaye() < 0) {
return self::STATUT_SURPLUS;
}
// payé
elseif ($this->getMontant() - $this->getMontantPaye() < 0.001) {
return self::STATUT_PAYEE;
}
// reste à payer
elseif ($this->getMontant() - $this->getMontantPaye() > 0.01) {
return self::STATUT_IMPAYEE;
}
}
public function getResumePanier() {
if (!isset($this->commandeProduits))
$this->commandeProduits = CommandeProduit::find()->where(['id_commande' => $this->id])->all();
$html = '';
$count = count($this->commandeProduits);
$i = 0;
foreach ($this->commandeProduits as $p) {
if (isset($p->produit)) {
$html .= $p->quantite . ' x ' . Html::encode($p->produit->nom);
if (++$i != $count)
$html .= '
';
}
}
return $html;
}
public function getResumePointVente() {
$html = '';
if (isset($this->pointVente)) {
$html .= '' . Html::encode($this->pointVente->nom) . ''
. '
' . Html::encode($this->pointVente->localite) . '';
if (strlen($this->commentaire_point_vente)) {
$html .= '