use common\models\Order ; | use common\models\Order ; | ||||
use common\models\Product ; | use common\models\Product ; | ||||
use common\models\SubscriptionSearch ; | |||||
class SubscriptionController extends BackendController | class SubscriptionController extends BackendController | ||||
{ | { | ||||
{ | { | ||||
// form | // form | ||||
$model = new SubscriptionForm; | $model = new SubscriptionForm; | ||||
$model->isAdmin = true ; | |||||
$model->id_producer = Producer::getId(); | $model->id_producer = Producer::getId(); | ||||
if($idOrder) { | if($idOrder) { | ||||
{ | { | ||||
// form | // form | ||||
$model = new SubscriptionForm; | $model = new SubscriptionForm; | ||||
$model->isAdmin = true ; | |||||
$subscription = Subscription::findOne($id); | $subscription = Subscription::findOne($id); | ||||
if ($subscription) { | if ($subscription) { | ||||
$model->id = $id; | $model->id = $id; | ||||
'unit' => $theProduct['unit'], | 'unit' => $theProduct['unit'], | ||||
'wording_unit' => Product::strUnit($theProduct['unit'], 'wording_short'), | 'wording_unit' => Product::strUnit($theProduct['unit'], 'wording_short'), | ||||
'step' => $theProduct['step'], | 'step' => $theProduct['step'], | ||||
'price' => $theProduct['price'] | |||||
] ; | ] ; | ||||
if(isset($theProduct['productSubscription'][0])) { | if(isset($theProduct['productSubscription'][0])) { | ||||
'unit' => $theProduct['productSubscription'][0]['unit'], | 'unit' => $theProduct['productSubscription'][0]['unit'], | ||||
'wording_unit' => Product::strUnit($theProduct['productSubscription'][0]['unit'], 'wording_short'), | 'wording_unit' => Product::strUnit($theProduct['productSubscription'][0]['unit'], 'wording_short'), | ||||
'step' => $theProduct['productSubscription'][0]['step'], | 'step' => $theProduct['productSubscription'][0]['step'], | ||||
'price' => $theProduct['productSubscription'][0]['price'], | |||||
] ; | ] ; | ||||
$theProduct['unit'] = $theProduct['productSubscription'][0]['unit'] ; | $theProduct['unit'] = $theProduct['productSubscription'][0]['unit'] ; | ||||
$theProduct['step'] = $theProduct['productSubscription'][0]['step'] ; | $theProduct['step'] = $theProduct['productSubscription'][0]['step'] ; | ||||
$theProduct['price'] = $theProduct['productSubscription'][0]['price'] ; | |||||
} | } | ||||
$theProduct['quantity'] = $theProduct['productSubscription'][0]['quantity'] * Product::$unitsArray[$theProduct['unit']]['coefficient'] ; | $theProduct['quantity'] = $theProduct['productSubscription'][0]['quantity'] * Product::$unitsArray[$theProduct['unit']]['coefficient'] ; | ||||
} | } |
<td>{{ product.name }}</td> | <td>{{ product.name }}</td> | ||||
<td> | <td> | ||||
<input type="hidden" :value="product.step" :name="'product_step_'+product.step" /> | <input type="hidden" :value="product.step" :name="'product_step_'+product.step" /> | ||||
<input type="hidden" :value="product.price" :name="'product_price_'+product.price" /> | |||||
<div class="input-group"> | <div class="input-group"> | ||||
<span class="input-group-btn"> | <span class="input-group-btn"> | ||||
<button class="btn btn-default" type="button" @click="changeQuantityProductSubscription(false, product)"><span class="glyphicon glyphicon-minus"></span></button> | <button class="btn btn-default" type="button" @click="changeQuantityProductSubscription(false, product)"><span class="glyphicon glyphicon-minus"></span></button> |
foreach($model->productSubscription as $productSubscription) | foreach($model->productSubscription as $productSubscription) | ||||
{ | { | ||||
if(isset($productSubscription->product)) { | if(isset($productSubscription->product)) { | ||||
$html .= Html::encode($productSubscription->product->name).' ('.$productSubscription->quantity.' '.Product::strUnit($productSubscription->unit, 'wording_short').')<br />' ; | |||||
$html .= Html::encode($productSubscription->product->name).' ('.($productSubscription->quantity * Product::$unitsArray[$productSubscription->unit]['coefficient']).' '.Product::strUnit($productSubscription->unit, 'wording_short').')<br />' ; | |||||
} | } | ||||
else { | else { | ||||
$html .= 'Produit non défini<br />' ; | $html .= 'Produit non défini<br />' ; | ||||
// aucun produit | // aucun produit | ||||
if(!count($model->productSubscription)) | if(!count($model->productSubscription)) | ||||
{ | { | ||||
$html .= '<span class="glyphicon glyphicon-warning-sign"></span> Aucun produit' ; | |||||
$html .= '<span class="glyphicon glyphicon-warning-sign"></span> Aucun produit<br />' ; | |||||
} | |||||
// des unités ne correspondent pas | |||||
if($model->hasUnitsNotMatch()) { | |||||
$html .= '<span class="glyphicon glyphicon-warning-sign" title="Des unités ne correspondent pas"></span>' ; | |||||
} | } | ||||
return $html ; | return $html ; |
for(var j = 0; j < this.products[i].units.length ; j ++) { | for(var j = 0; j < this.products[i].units.length ; j ++) { | ||||
if(this.products[i].unit == this.products[i].units[j].unit) { | if(this.products[i].unit == this.products[i].units[j].unit) { | ||||
this.products[i].step = this.products[i].units[j].step ; | this.products[i].step = this.products[i].units[j].step ; | ||||
this.products[i].price = this.products[i].units[j].price ; | |||||
} | } | ||||
} | } | ||||
} | } |
} | } | ||||
} | } | ||||
} | } | ||||
/** | |||||
* Retourne true si des unités des ProductSubscription ne correspondent pas | |||||
* aux Product. | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
public function hasUnitsNotMatch() | |||||
{ | |||||
if(isset($this->productSubscription) && is_array($this->productSubscription)) { | |||||
foreach($this->productSubscription as $productSubscription) { | |||||
if(isset($productSubscription->product) && $productSubscription->unit != $productSubscription->product->unit) { | |||||
return true ; | |||||
} | |||||
} | |||||
} | |||||
return false ; | |||||
} | |||||
} | } |
*/ | */ | ||||
class SubscriptionForm extends Model | class SubscriptionForm extends Model | ||||
{ | { | ||||
public $isAdmin = false ; | |||||
public $id; | public $id; | ||||
public $id_user; | public $id_user; | ||||
public $username; | public $username; | ||||
// produits | // produits | ||||
if ($this->id) { | if ($this->id) { | ||||
$productsSubscriptionsArray = ProductSubscription::findAll(['id_subscription' => $this->id]) ; | |||||
ProductSubscription::deleteAll(['id_subscription' => $this->id]); | ProductSubscription::deleteAll(['id_subscription' => $this->id]); | ||||
} | } | ||||
$idProduct = (int) str_replace('product_', '', $nameInput); | $idProduct = (int) str_replace('product_', '', $nameInput); | ||||
$product = Product::findOne($idProduct) ; | $product = Product::findOne($idProduct) ; | ||||
$subscriptionProduct = new ProductSubscription; | |||||
$subscriptionProduct->id_subscription = $subscription->id; | |||||
$subscriptionProduct->id_product = $idProduct; | |||||
$newProductSubscription = new ProductSubscription; | |||||
$newProductSubscription->id_subscription = $subscription->id; | |||||
$newProductSubscription->id_product = $idProduct; | |||||
$unit = (Yii::$app->getRequest()->post('product_unit_'.$idProduct)) ? Yii::$app->getRequest()->post('product_unit_'.$idProduct) : $product->unit ; | |||||
$subscriptionProduct->unit = $unit; | |||||
$step = (Yii::$app->getRequest()->post('product_step_'.$idProduct)) ? Yii::$app->getRequest()->post('product_step_'.$idProduct) : $product->step ; | |||||
$subscriptionProduct->step = $step; | |||||
if($this->isAdmin) { | |||||
$unit = (Yii::$app->getRequest()->post('product_unit_'.$idProduct)) ? Yii::$app->getRequest()->post('product_unit_'.$idProduct) : $product->unit ; | |||||
$newProductSubscription->unit = $unit; | |||||
$step = (Yii::$app->getRequest()->post('product_step_'.$idProduct)) ? Yii::$app->getRequest()->post('product_step_'.$idProduct) : $product->step ; | |||||
$newProductSubscription->step = $step; | |||||
$price = (Yii::$app->getRequest()->post('product_price_'.$idProduct)) ? Yii::$app->getRequest()->post('product_price_'.$idProduct) : $product->price ; | |||||
$newProductSubscription->price = $price; | |||||
} | |||||
else { | |||||
foreach($productsSubscriptionsArray as $productSubscription) { | |||||
if($productSubscription->id_product == $idProduct) { | |||||
$newProductSubscription->unit = $productSubscription->unit ; | |||||
$newProductSubscription->step = $productSubscription->step ; | |||||
$newProductSubscription->price = $productSubscription->price ; | |||||
} | |||||
} | |||||
if(!$newProductSubscription->unit) { | |||||
$newProductSubscription->unit = $product->unit ; | |||||
} | |||||
if(!$newProductSubscription->step) { | |||||
$newProductSubscription->step = $product->step; | |||||
} | |||||
if(!$newProductSubscription->price) { | |||||
$newProductSubscription->price = $product->price; | |||||
} | |||||
} | |||||
$subscriptionProduct->quantity = $quantity / Product::$unitsArray[$unit]['coefficient']; | |||||
$newProductSubscription->quantity = $quantity / Product::$unitsArray[$newProductSubscription->unit]['coefficient']; | |||||
$subscriptionProduct->save(); | |||||
$newProductSubscription->save(); | |||||
} | } | ||||
} | } | ||||
<?php | |||||
use yii\db\Migration; | |||||
use yii\db\mysql\Schema; | |||||
class m190515_122438_ajout_champs_price_product_subscription extends Migration { | |||||
public function up() { | |||||
$this->addColumn('product_subscription', 'price', Schema::TYPE_FLOAT. ' DEFAULT 0') ; | |||||
$productsSubscriptionsArray = common\models\ProductSubscription::find()->all() ; | |||||
$productsArray = common\models\Product::find()->all() ; | |||||
foreach($productsSubscriptionsArray as $productSubscription) { | |||||
foreach($productsArray as $product) { | |||||
if($productSubscription->id_product == $product->id) { | |||||
$productSubscription->price = $product->price ; | |||||
$productSubscription->save() ; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
public function down() { | |||||
$this->dropColumn('product_subscription', 'price') ; | |||||
} | |||||
} |
use common\models\SubscriptionForm ; | use common\models\SubscriptionForm ; | ||||
use common\models\SubscriptionSearch ; | use common\models\SubscriptionSearch ; | ||||
use common\models\Product ; | |||||
class SubscriptionController extends ProducerBaseController | class SubscriptionController extends ProducerBaseController | ||||
{ | { | ||||
if($idSubscription > 0) { | if($idSubscription > 0) { | ||||
// Quantités produit de l'abonnement | |||||
$productQuantitiesArray = [] ; | |||||
$arrayProductsSubscription = ProductSubscription::searchAll([ | $arrayProductsSubscription = ProductSubscription::searchAll([ | ||||
'id_subscription' => $idSubscription | 'id_subscription' => $idSubscription | ||||
]) ; | ]) ; | ||||
if(count($arrayProductsSubscription)) { | |||||
foreach ($arrayProductsSubscription as $productSubscription) { | |||||
$productQuantitiesArray[$productSubscription->id_product] = $productSubscription->quantity; | |||||
} | |||||
} | |||||
} | } | ||||
// Produits | // Produits | ||||
$productsArray = Product::searchAll() ; | $productsArray = Product::searchAll() ; | ||||
$indexProduct = 0 ; | $indexProduct = 0 ; | ||||
foreach($productsArray as &$product) { | foreach($productsArray as &$product) { | ||||
$quantity = 0 ; | $quantity = 0 ; | ||||
if(isset($productQuantitiesArray) && count($productQuantitiesArray) && isset($productQuantitiesArray[$product->id])) { | |||||
$quantity = $productQuantitiesArray[$product->id] ; | |||||
$coefficientUnit = Product::$unitsArray[$product->unit]['coefficient'] ; | |||||
if(isset($arrayProductsSubscription) && count($arrayProductsSubscription)) { | |||||
foreach ($arrayProductsSubscription as $productSubscription) { | |||||
if($product->id == $productSubscription->id_product) { | |||||
$coefficientUnit = Product::$unitsArray[$productSubscription->unit]['coefficient'] ; | |||||
$quantity = $productSubscription->quantity * $coefficientUnit; | |||||
$product->step = $productSubscription->step; | |||||
$product->unit = $productSubscription->unit; | |||||
$product->price = $productSubscription->price; | |||||
} | |||||
} | |||||
} | } | ||||
$product = array_merge( | $product = array_merge( | ||||
$product->getAttributes(), | $product->getAttributes(), | ||||
[ | [ | ||||
'index' => $indexProduct ++, | 'index' => $indexProduct ++, | ||||
'quantity_form' => $quantity | |||||
'quantity_form' => $quantity, | |||||
'coefficient_unit' => $coefficientUnit, | |||||
'wording_unit' => Product::strUnit($product->unit, 'wording_unit', true), | |||||
'wording_short' => Product::strUnit($product->unit, 'wording_short'), | |||||
] | ] | ||||
) ; | ) ; | ||||
} | } |
<div class="recipe" v-if="product.recipe.length">{{ product.recipe }}</div> | <div class="recipe" v-if="product.recipe.length">{{ product.recipe }}</div> | ||||
</td> | </td> | ||||
<td class="price-unit"> | <td class="price-unit"> | ||||
{{ formatPrice(product.price) }} | |||||
{{ formatPrice(product.price) }}<br /><span class="unit">{{ product.wording_unit }}</span> | |||||
</td> | </td> | ||||
<td class="quantity"> | <td class="quantity"> | ||||
<div class="input-group"> | <div class="input-group"> | ||||
<input type="hidden" :value="product.step" :name="'product_step_'+product.step" /> | |||||
<input type="hidden" :value="product.unit" :name="'product_unit_'+product.unit" /> | |||||
<span class="input-group-btn"> | <span class="input-group-btn"> | ||||
<button class="btn btn-default" type="button" @click="productQuantityClick(product, -1)"><span class="glyphicon glyphicon-minus"></span></button> | |||||
<button class="btn btn-default" type="button" @click="productQuantityClick(product, -product.step)"><span class="glyphicon glyphicon-minus"></span></button> | |||||
</span> | </span> | ||||
<input type="text" v-model="product.quantity_form" :class="'form-control '+((product.quantity_form > 0) ? 'has-quantity' : '')"> | <input type="text" v-model="product.quantity_form" :class="'form-control '+((product.quantity_form > 0) ? 'has-quantity' : '')"> | ||||
<span class="input-group-addon">{{ product.wording_short }}</span> | |||||
<span class="input-group-btn"> | <span class="input-group-btn"> | ||||
<button class="btn btn-default" type="button" @click="productQuantityClick(product, 1)"><span class="glyphicon glyphicon-plus"></span></button> | |||||
<button class="btn btn-default" type="button" @click="productQuantityClick(product, product.step)"><span class="glyphicon glyphicon-plus"></span></button> | |||||
</span> | </span> | ||||
</div> | </div> | ||||
</td> | </td> | ||||
<td class="price-total"> | <td class="price-total"> | ||||
{{ formatPrice(product.price * product.quantity_form) }} | |||||
{{ formatPrice(product.price * (product.quantity_form / product.coefficient_unit )) }} | |||||
</td> | </td> | ||||
</tr> | </tr> | ||||
<tr class="total"> | <tr class="total"> |
use yii\helpers\Html; | use yii\helpers\Html; | ||||
use yii\grid\GridView; | use yii\grid\GridView; | ||||
use common\models\Product ; | |||||
$this->setTitle('Abonnements') ; | $this->setTitle('Abonnements') ; | ||||
$this->addButton(['label' => '<span class="glyphicon glyphicon-plus"></span> Ajouter', 'url' => 'subscription/form', 'class' => 'btn btn-primary']) ; | $this->addButton(['label' => '<span class="glyphicon glyphicon-plus"></span> Ajouter', 'url' => 'subscription/form', 'class' => 'btn btn-primary']) ; | ||||
foreach($model->productSubscription as $productSubscription) | foreach($model->productSubscription as $productSubscription) | ||||
{ | { | ||||
if(isset($productSubscription->product)) { | if(isset($productSubscription->product)) { | ||||
$html .= $productSubscription->quantity . ' x '.Html::encode($productSubscription->product->name).'<br />' ; | |||||
$html .= Html::encode($productSubscription->product->name).' ('.($productSubscription->quantity * Product::$unitsArray[$productSubscription->unit]['coefficient']) . ' '.Product::strUnit($productSubscription->unit, 'wording_short').')<br />' ; | |||||
} | } | ||||
else { | else { | ||||
$html .= 'Produit non défini<br />' ; | $html .= 'Produit non défini<br />' ; |
/* line 73, ../sass/subscription/_form.scss */ | /* line 73, ../sass/subscription/_form.scss */ | ||||
.subscription-create .subscription-form .products td.quantity, | .subscription-create .subscription-form .products td.quantity, | ||||
.subscription-update .subscription-form .products td.quantity { | .subscription-update .subscription-form .products td.quantity { | ||||
width: 150px; | |||||
width: 180px; | |||||
} | } | ||||
/* line 76, ../sass/subscription/_form.scss */ | /* line 76, ../sass/subscription/_form.scss */ | ||||
.subscription-create .subscription-form .products td.quantity input, | .subscription-create .subscription-form .products td.quantity input, | ||||
.subscription-update .subscription-form .products td.quantity input { | .subscription-update .subscription-form .products td.quantity input { | ||||
text-align: center; | text-align: center; | ||||
border-right: 0px none; | |||||
} | } | ||||
/* line 79, ../sass/subscription/_form.scss */ | /* line 79, ../sass/subscription/_form.scss */ | ||||
.subscription-create .subscription-form .products td.quantity input.has-quantity, | .subscription-create .subscription-form .products td.quantity input.has-quantity, | ||||
.subscription-update .subscription-form .products td.quantity input.has-quantity { | .subscription-update .subscription-form .products td.quantity input.has-quantity { | ||||
font-weight: bold; | font-weight: bold; | ||||
} | } | ||||
/* line 85, ../sass/subscription/_form.scss */ | |||||
/* line 84, ../sass/subscription/_form.scss */ | |||||
.subscription-create .subscription-form .products td.quantity .input-group-addon, | |||||
.subscription-update .subscription-form .products td.quantity .input-group-addon { | |||||
background-color: white; | |||||
padding-left: 0px; | |||||
border-left: 0px none; | |||||
} | |||||
/* line 91, ../sass/subscription/_form.scss */ | |||||
.subscription-create .subscription-form .products .name, | .subscription-create .subscription-form .products .name, | ||||
.subscription-update .subscription-form .products .name { | .subscription-update .subscription-form .products .name { | ||||
font-family: "capsuularegular"; | font-family: "capsuularegular"; | ||||
font-size: 20px; | font-size: 20px; | ||||
color: black; | color: black; | ||||
} | } | ||||
/* line 91, ../sass/subscription/_form.scss */ | |||||
/* line 97, ../sass/subscription/_form.scss */ | |||||
.subscription-create .subscription-form .products .description, | .subscription-create .subscription-form .products .description, | ||||
.subscription-update .subscription-form .products .description { | .subscription-update .subscription-form .products .description { | ||||
font-style: italic; | font-style: italic; | ||||
} | } | ||||
/* line 95, ../sass/subscription/_form.scss */ | |||||
/* line 101, ../sass/subscription/_form.scss */ | |||||
.subscription-create .subscription-form .products .recipe, | .subscription-create .subscription-form .products .recipe, | ||||
.subscription-update .subscription-form .products .recipe { | .subscription-update .subscription-form .products .recipe { | ||||
font-size: 12px; | font-size: 12px; | ||||
} | } | ||||
/* line 99, ../sass/subscription/_form.scss */ | |||||
/* line 105, ../sass/subscription/_form.scss */ | |||||
.subscription-create .subscription-form .products .price-unit, .subscription-create .subscription-form .products .price-total, | .subscription-create .subscription-form .products .price-unit, .subscription-create .subscription-form .products .price-total, | ||||
.subscription-update .subscription-form .products .price-unit, | .subscription-update .subscription-form .products .price-unit, | ||||
.subscription-update .subscription-form .products .price-total { | .subscription-update .subscription-form .products .price-total { | ||||
text-align: center; | text-align: center; | ||||
width: 150px; | width: 150px; | ||||
} | } | ||||
/* line 104, ../sass/subscription/_form.scss */ | |||||
/* line 110, ../sass/subscription/_form.scss */ | |||||
.subscription-create .subscription-form .products .unit, | |||||
.subscription-update .subscription-form .products .unit { | |||||
color: gray; | |||||
font-size: 13px; | |||||
} | |||||
/* line 115, ../sass/subscription/_form.scss */ | |||||
.subscription-create .subscription-form .products tr.total .price-total, | .subscription-create .subscription-form .products tr.total .price-total, | ||||
.subscription-update .subscription-form .products tr.total .price-total { | .subscription-update .subscription-form .products tr.total .price-total { | ||||
text-align: center; | text-align: center; |
var price = 0 ; | var price = 0 ; | ||||
for(var key in this.products) { | for(var key in this.products) { | ||||
if(this.products[key].quantity_form > 0) { | if(this.products[key].quantity_form > 0) { | ||||
price += this.products[key].quantity_form * this.products[key].price ; | |||||
price += (this.products[key].quantity_form / this.products[key].coefficient_unit) * this.products[key].price ; | |||||
} | } | ||||
} | } | ||||
if(format) { | if(format) { |
} | } | ||||
td.quantity { | td.quantity { | ||||
width: 150px ; | |||||
width: 180px ; | |||||
input { | input { | ||||
text-align: center ; | text-align: center ; | ||||
border-right: 0px none ; | |||||
&.has-quantity { | &.has-quantity { | ||||
font-weight: bold ; | font-weight: bold ; | ||||
} | } | ||||
} | } | ||||
.input-group-addon { | |||||
background-color: white ; | |||||
padding-left: 0px ; | |||||
border-left: 0px none ; | |||||
} | |||||
} | } | ||||
.name { | .name { | ||||
width: 150px ; | width: 150px ; | ||||
} | } | ||||
.unit { | |||||
color: gray ; | |||||
font-size: 13px ; | |||||
} | |||||
tr.total .price-total { | tr.total .price-total { | ||||
text-align: center ; | text-align: center ; | ||||
font-size: 20px ; | font-size: 20px ; |