Ver código fonte

[Backend] Commandes automatiques (création des commandes)

Création automatique des commandes lors de l'activation des productions.
master
keun 7 anos atrás
pai
commit
242be89713
3 arquivos alterados com 113 adições e 0 exclusões
  1. +7
    -0
      backend/controllers/CommandeController.php
  2. +5
    -0
      common/models/Commande.php
  3. +101
    -0
      common/models/CommandeAuto.php

+ 7
- 0
backend/controllers/CommandeController.php Ver arquivo

@@ -17,6 +17,7 @@ use common\helpers\CSV;
use common\models\User;
use kartik\mpdf\Pdf;
use common\models\CommandeAutoForm ;
use common\models\CommandeAuto ;

class CommandeController extends BackendController {

@@ -803,6 +804,12 @@ class CommandeController extends BackendController {
$production->actif = $actif;
$production->save();

// add commandes automatiques
if($actif)
{
CommandeAuto::addAll($date) ;
}
$this->redirect(['index', 'date' => $date]);
}


+ 5
- 0
common/models/Commande.php Ver arquivo

@@ -22,6 +22,11 @@ class Commande extends \yii\db\ActiveRecord
var $montant_vrac = 0 ;
var $poids_pain = 0 ;
var $poids_vrac = 0 ;
const TYPE_AUTO = 'auto' ;
const TYPE_USER = 'user' ;
const TYPE_ADMIN = 'admin' ;
/**
* @inheritdoc

+ 101
- 0
common/models/CommandeAuto.php Ver arquivo

@@ -6,6 +6,8 @@ use Yii;

use common\models\Etablissement ;
use common\models\PointVente ;
use common\models\Commande ;
use common\models\CommandeProduit ;

/**
* This is the model class for table "commande_auto".
@@ -13,6 +15,7 @@ use common\models\PointVente ;
* @property integer $id
* @property integer $id_user
* @property integer $id_etablissement
* @property integer $id_point_vente
* @property string $date_debut
* @property string $date_fin
* @property integer $lundi
@@ -88,4 +91,102 @@ class CommandeAuto extends \yii\db\ActiveRecord
{
return $this->hasMany(CommandeAutoProduit::className(), ['id_commande_auto'=>'id'])->with('produit') ;
}
public static function getAll($date)
{
$date = date('Y-m-d', strtotime($date)) ;
// commandes auto
$commandes_auto = self::find()
->with('commandeAutoProduit')
->where(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
->all() ;
$arr_commandes_auto = [] ;
foreach($commandes_auto as $c)
{
// vérif dates
if($date >= $c->date_debut &&
(!$c->date_fin || $date <= $c->date_fin))
{
// périodicite
$nb_jours = (strtotime($date) - strtotime($c->date_debut)) / (24*60*60) ;
if($nb_jours % ($c->periodicite_semaine * 7) < 7 )
{
// jour de la semaine
$jour = date('N', strtotime($date)) ;
switch($jour)
{
case 1 : $jour = 'lundi' ;
case 2 : $jour = 'mardi' ;
case 3 : $jour = 'mercredi' ;
case 4 : $jour = 'jeudi' ;
case 5 : $jour = 'vendredi' ;
case 6 : $jour = 'samedi' ;
case 7 : $jour = 'dimanche' ;
}
if($c->$jour)
{
$arr_commandes_auto[] = $c ;
}
}
}
}
return $arr_commandes_auto ;
}
public static function addAll($date)
{
// production
$production = Production::findOne(['date' => date('Y-m-d',strtotime($date))]) ;
if($production)
{
$count_commandes_prod = Commande::find()
->where(['id_production' => $production->id])
->count() ;
if(!$count_commandes_prod)
{
$commandes_auto = self::getAll($date) ;
foreach($commandes_auto as $c)
{
$c->add($date) ;
}
}
}
}
public function add($date)
{
// production
$production = Production::findOne(['date' => date('Y-m-d',strtotime($date))]) ;
if($production)
{
// commande
$commande = new Commande ;
$commande->id_user = $this->id_user ;
$commande->date = date('Y-m-d H:i:s') ;
$commande->type = Commande::TYPE_AUTO ;
$commande->id_point_vente = $this->id_point_vente ;
$commande->id_production = $production->id ;
$commande->save() ;
// produits
foreach($this->commandeAutoProduit as $commande_auto_produit)
{
$commande_produit = new CommandeProduit ;
$commande_produit->id_commande = $commande->id ;
$commande_produit->id_produit = $commande_auto_produit->produit->id ;
$commande_produit->quantite = $commande_auto_produit->quantite ;
$commande_produit->prix = $commande_auto_produit->produit->prix ;
$commande_produit->save() ;
}
}
}
}

Carregando…
Cancelar
Salvar