Browse Source

[Backend] Bons de livraison : désactiver anciens BL Terre de Pains #989

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
e49bc26708
3 changed files with 187 additions and 163 deletions
  1. +160
    -162
      backend/controllers/DocumentController.php
  2. +1
    -1
      common/models/Document.php
  3. +26
    -0
      console/migrations/m230405_090528_add_field_delivery_note_ignore_when_billing.php

+ 160
- 162
backend/controllers/DocumentController.php View File

public function behaviors() public function behaviors()
{ {
return [ return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
], ],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action) {
return User::hasAccessBackend();
}
]
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action) {
return User::hasAccessBackend();
}
]
], ],
],
]; ];
} }


Invoice::find()->where(['status' => Document::STATUS_VALID])->all() Invoice::find()->where(['status' => Document::STATUS_VALID])->all()
); );


foreach($validatedDocumentsArray as $document) {
if(!file_exists($document->getFilenameComplete())) {
foreach ($validatedDocumentsArray as $document) {
if (!file_exists($document->getFilenameComplete())) {
$document->generatePdf(Pdf::DEST_FILE); $document->generatePdf(Pdf::DEST_FILE);
} }
} }
} }


return $this->render('/document/create', [ return $this->render('/document/create', [
'title' => $this->getTitle('Ajouter'),
'typeDocument' => $this->getDocumentType(),
'model' => $model,
'title' => $this->getTitle('Ajouter'),
'typeDocument' => $this->getDocumentType(),
'model' => $model,
]); ]);
} }


if ($model->deliveryNotes && is_array($model->deliveryNotes) && count($model->deliveryNotes)) { if ($model->deliveryNotes && is_array($model->deliveryNotes) && count($model->deliveryNotes)) {
foreach ($model->deliveryNotes as $key => $idDeliveryNote) { foreach ($model->deliveryNotes as $key => $idDeliveryNote) {
Order::updateAll([ Order::updateAll([
'id_invoice' => $model->id
], [
'id_delivery_note' => $idDeliveryNote
]);
'id_invoice' => $model->id
], [
'id_delivery_note' => $idDeliveryNote
]);
} }
} }
} }
} }


return $this->render('/document/update', [ return $this->render('/document/update', [
'title' => $this->getTitle('Modifier'),
'typeDocument' => $this->getDocumentType(),
'model' => $model,
'title' => $this->getTitle('Modifier'),
'typeDocument' => $this->getDocumentType(),
'model' => $model,
]); ]);
} }




if ($this->getClass() == 'DeliveryNote') { if ($this->getClass() == 'DeliveryNote') {
Order::updateAll([ Order::updateAll([
'order.id_delivery_note' => null
], [
'order.id_delivery_note' => $id
]);
'order.id_delivery_note' => null
], [
'order.id_delivery_note' => $id
]);
} }


if ($this->getClass() == 'Quotation') { if ($this->getClass() == 'Quotation') {
Order::updateAll([ Order::updateAll([
'order.id_quotation' => null
], [
'order.id_quotation' => $id
]);
'order.id_quotation' => null
], [
'order.id_quotation' => $id
]);
} }


if ($this->getClass() == 'Invoice') { if ($this->getClass() == 'Invoice') {
Order::updateAll([ Order::updateAll([
'order.id_invoice' => null
], [
'order.id_invoice' => $id
]);
'order.id_invoice' => null
], [
'order.id_invoice' => $id
]);
} }


Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('delete', $model)); Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('delete', $model));
'Code Classification vente', 'Code Classification vente',
]; ];


foreach($document->getProductsOrders() as $productOrderArray) {
foreach($productOrderArray as $productOrder) {
foreach ($document->getProductsOrders() as $productOrderArray) {
foreach ($productOrderArray as $productOrder) {


$price = $productOrder->getPrice() ;
if($document->isInvoicePrice() && $productOrder->getInvoicePrice()) {
$price = $productOrder->getInvoicePrice() ;
$price = $productOrder->getPrice();
if ($document->isInvoicePrice() && $productOrder->getInvoicePrice()) {
$price = $productOrder->getInvoicePrice();
} }


$typeTotal = $document->isInvoicePrice() ? Order::INVOICE_AMOUNT_TOTAL : Order::AMOUNT_TOTAL; $typeTotal = $document->isInvoicePrice() ? Order::INVOICE_AMOUNT_TOTAL : Order::AMOUNT_TOTAL;


// nom fichier // nom fichier
$reference = $document->id; $reference = $document->id;
if($document->reference && strlen($document->reference)) {
if ($document->reference && strlen($document->reference)) {
$reference = $document->reference; $reference = $document->reference;
} }


// status // status
$status = ''; $status = '';
if($document->isStatusDraft()) {
if ($document->isStatusDraft()) {
$status = 'brouillon_'; $status = 'brouillon_';
} }


CSV::downloadSendHeaders(strtolower($this->getDocumentType()).'_' . $status . $reference . '.csv');
CSV::downloadSendHeaders(strtolower($this->getDocumentType()) . '_' . $status . $reference . '.csv');
echo CSV::array2csv($datas); echo CSV::array2csv($datas);
die(); die();
} }
Yii::$app->getSession()->setFlash('danger', $this->getFlashMessage('send', $document)); Yii::$app->getSession()->setFlash('danger', $this->getFlashMessage('send', $document));
} }


if($backUpdateForm) {
if ($backUpdateForm) {
return $this->redirect([$this->getControllerUrl() . '/update', 'id' => $id]); return $this->redirect([$this->getControllerUrl() . '/update', 'id' => $id]);
}
else {
} else {
return $this->redirect([$this->getControllerUrl() . '/index']); return $this->redirect([$this->getControllerUrl() . '/index']);
} }
} }


if ($idUser > 0) { if ($idUser > 0) {
$user = User::searchOne([ $user = User::searchOne([
'id' => $idUser
]);
'id' => $idUser
]);


if ($user) { if ($user) {
$document = null; $document = null;
if (Document::isValidClass($classDocument)) { if (Document::isValidClass($classDocument)) {
$document = $classDocument::searchOne([ $document = $classDocument::searchOne([
'id' => $idDocument,
'id_user' => $idUser
]);
'id' => $idDocument,
'id_user' => $idUser
]);
} }


if ($document && $document->id_user == $user->id) { if ($document && $document->id_user == $user->id) {
} }


$json = [ $json = [
'return' => 'success',
'address' => $address
'return' => 'success',
'address' => $address
]; ];


if ($classDocument == 'Invoice') { if ($classDocument == 'Invoice') {
'join_with' => ['user AS user_delivery_note', 'orders', 'producer'] 'join_with' => ['user AS user_delivery_note', 'orders', 'producer']
]; ];
$deliveryNotesCreateArray = DeliveryNote::searchAll([ $deliveryNotesCreateArray = DeliveryNote::searchAll([
'id_user' => $user->id,
'status' => Document::STATUS_VALID
], $options);
'id_user' => $user->id,
'status' => Document::STATUS_VALID,
'ignore_when_billing' => null
], $options);
$deliveryNotesUpdateArray = DeliveryNote::searchAll([ $deliveryNotesUpdateArray = DeliveryNote::searchAll([
'id_user' => $user->id,
'status' => Document::STATUS_VALID,
'order.id_invoice' => $idDocument
], $options);
'id_user' => $user->id,
'status' => Document::STATUS_VALID,
'order.id_invoice' => $idDocument,
'ignore_when_billing' => null
], $options);
$json['delivery_note_create_array'] = $this->initDeliveryNoteArray('create', $deliveryNotesCreateArray); $json['delivery_note_create_array'] = $this->initDeliveryNoteArray('create', $deliveryNotesCreateArray);
$json['delivery_note_update_array'] = $this->initDeliveryNoteArray('update', $deliveryNotesUpdateArray); $json['delivery_note_update_array'] = $this->initDeliveryNoteArray('update', $deliveryNotesUpdateArray);
} }
{ {
$deliveryNoteArray = []; $deliveryNoteArray = [];
$isCreate = false; $isCreate = false;
if($type == 'create') {
if ($type == 'create') {
$isCreate = true; $isCreate = true;
} }


if($deliveryNoteArrayResults) {
if ($deliveryNoteArrayResults) {
foreach ($deliveryNoteArrayResults as $deliveryNote) { foreach ($deliveryNoteArrayResults as $deliveryNote) {
$deliveryNoteData = $this->addDeliveryNoteToArray($deliveryNote, $isCreate); $deliveryNoteData = $this->addDeliveryNoteToArray($deliveryNote, $isCreate);
if($deliveryNoteData) {
if ($deliveryNoteData) {
$deliveryNoteArray[] = $deliveryNoteData; $deliveryNoteArray[] = $deliveryNoteData;
} }
} }


if ($id > 0 && Document::isValidClass($classDocument)) { if ($id > 0 && Document::isValidClass($classDocument)) {
$document = $classDocument::searchOne([ $document = $classDocument::searchOne([
'id' => $id
]);
'id' => $id
]);


if ($document) { if ($document) {
$document->changeStatus(Document::STATUS_VALID); $document->changeStatus(Document::STATUS_VALID);


Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('validate', $document)); Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('validate', $document));


if($backUpdateForm) {
if ($backUpdateForm) {
return $this->redirect([$this->getControllerUrl() . '/update', 'id' => $id]); return $this->redirect([$this->getControllerUrl() . '/update', 'id' => $id]);
}
else {
} else {
return $this->redirect([$this->getControllerUrl() . '/index']); return $this->redirect([$this->getControllerUrl() . '/index']);
} }
} }


if ($idDocument > 0 && Document::isValidClass($classDocument)) { if ($idDocument > 0 && Document::isValidClass($classDocument)) {
$document = $classDocument::searchOne([ $document = $classDocument::searchOne([
'id' => $idDocument
]);
'id' => $idDocument
]);


if ($document) { if ($document) {
$document->changeStatus(Document::STATUS_VALID); $document->changeStatus(Document::STATUS_VALID);
$document->save(); $document->save();
return [ return [
'return' => 'success',
'alert' => [
'type' => 'success',
'message' => 'Document validé'
]
'return' => 'success',
'alert' => [
'type' => 'success',
'message' => 'Document validé'
]
]; ];
} }
} }


return [ return [
'return' => 'error',
'alert' => [
'type' => 'danger',
'message' => 'Une erreur est survenue lors de la validation du document.'
]
'return' => 'error',
'alert' => [
'type' => 'danger',
'message' => 'Une erreur est survenue lors de la validation du document.'
]
]; ];
} }




if ($idDocument > 0 && Document::isValidClass($classDocument)) { if ($idDocument > 0 && Document::isValidClass($classDocument)) {
$document = $classDocument::searchOne([ $document = $classDocument::searchOne([
'id' => $idDocument
]);
'id' => $idDocument
]);


if ($document) { if ($document) {
$productsArray = Product::searchAll([], [ $productsArray = Product::searchAll([], [
'orderby' => 'product.order ASC'
'orderby' => 'product.order ASC'
]); ]);


$ordersArray = []; $ordersArray = [];
]); ]);
} }
$ordersArray[$order->id] = array_merge( $ordersArray[$order->id] = array_merge(
$order->getAttributes(),
[
'username' => $order->getUsername(),
'distribution_date' => isset($order->distribution) ? date(
'd/m/Y',
strtotime(
$order->distribution->date
)
) : null,
'point_sale_name' => isset($order->pointSale) ? $order->pointSale->name : null,
'productOrder' => $productsOrderArray,
]
$order->getAttributes(),
[
'username' => $order->getUsername(),
'distribution_date' => isset($order->distribution) ? date(
'd/m/Y',
strtotime(
$order->distribution->date
)
) : null,
'point_sale_name' => isset($order->pointSale) ? $order->pointSale->name : null,
'productOrder' => $productsOrderArray,
]
); );
} }


$userProducer = UserProducer::searchOne([ $userProducer = UserProducer::searchOne([
'id_user' => $document->user->id,
'id_producer' => GlobalParam::getCurrentProducerId()
]);
'id_user' => $document->user->id,
'id_producer' => GlobalParam::getCurrentProducerId()
]);
$pointSale = PointSale::searchOne([ $pointSale = PointSale::searchOne([
'id_user' => $document->user->id
]);
'id_user' => $document->user->id
]);


$productsArray = yii\helpers\ArrayHelper::map( $productsArray = yii\helpers\ArrayHelper::map(
$productsArray,
'order',
function ($product) use ($document, $userProducer, $pointSale) {
return array_merge($product->getAttributes(), [
'unit_coefficient' => Product::$unitsArray[$product->unit]['coefficient'],
'prices' => $product->getPriceArray($userProducer->user, $pointSale),
'wording_unit' => $product->wording_unit,
'tax_rate' => $product->taxRate->value
]);
}
$productsArray,
'order',
function ($product) use ($document, $userProducer, $pointSale) {
return array_merge($product->getAttributes(), [
'unit_coefficient' => Product::$unitsArray[$product->unit]['coefficient'],
'prices' => $product->getPriceArray($userProducer->user, $pointSale),
'wording_unit' => $product->wording_unit,
'tax_rate' => $product->taxRate->value
]);
}
); );


return [ return [
'return' => 'success',
'tax_rate_producer' => GlobalParam::getCurrentProducer()->taxRate->value,
'document' => array_merge($document->getAttributes(), [
'html_label' => $document->getHtmlLabel(),
'class' => $document->getClass()
]),
'id_user' => $document->user->id,
'products' => $productsArray,
'orders' => $ordersArray,
'total' => ($document->getClass() == 'Invoice' || $document->getClass(
) == 'DeliveryNote') ? $document->getAmount(
Order::INVOICE_AMOUNT_TOTAL
) : $document->getAmount(Order::AMOUNT_TOTAL),
'total_with_tax' => ($document->getClass() == 'Invoice' || $document->getClass(
) == 'DeliveryNote') ? $document->getAmountWithTax(
Order::INVOICE_AMOUNT_TOTAL
) : $document->getAmountWithTax(Order::AMOUNT_TOTAL),
'invoice_url' => ($document->getClass() == 'DeliveryNote' && $document->getInvoice()) ? Yii::$app->urlManager->createUrl(['invoice/update', 'id' => $document->getInvoice()->id]) : null
'return' => 'success',
'tax_rate_producer' => GlobalParam::getCurrentProducer()->taxRate->value,
'document' => array_merge($document->getAttributes(), [
'html_label' => $document->getHtmlLabel(),
'class' => $document->getClass()
]),
'id_user' => $document->user->id,
'products' => $productsArray,
'orders' => $ordersArray,
'total' => ($document->getClass() == 'Invoice' || $document->getClass() == 'DeliveryNote') ? $document->getAmount(
Order::INVOICE_AMOUNT_TOTAL
) : $document->getAmount(Order::AMOUNT_TOTAL),
'total_with_tax' => ($document->getClass() == 'Invoice' || $document->getClass() == 'DeliveryNote') ? $document->getAmountWithTax(
Order::INVOICE_AMOUNT_TOTAL
) : $document->getAmountWithTax(Order::AMOUNT_TOTAL),
'invoice_url' => ($document->getClass() == 'DeliveryNote' && $document->getInvoice()) ? Yii::$app->urlManager->createUrl(['invoice/update', 'id' => $document->getInvoice()->id]) : null
]; ];
} }
} }


if (Document::isValidClass($classDocument)) { if (Document::isValidClass($classDocument)) {
$document = $classDocument::searchOne([ $document = $classDocument::searchOne([
'id' => $idDocument
]);
'id' => $idDocument
]);
$product = Product::searchOne([ $product = Product::searchOne([
'id' => $idProduct
]);
'id' => $idProduct
]);


if ($document && $product) { if ($document && $product) {
if (count($document->orders) == 0) { if (count($document->orders) == 0) {
$productOrder->save(); $productOrder->save();


return [ return [
'return' => 'success',
'alert' => [
'type' => 'success',
'message' => 'Produit ajouté'
]
'return' => 'success',
'alert' => [
'type' => 'success',
'message' => 'Produit ajouté'
]
]; ];
} }
} }
} }


return [ return [
'return' => 'error',
'alert' => [
'type' => 'danger',
'message' => 'Une erreur est survenue lors de la suppression du produit.'
]
'return' => 'error',
'alert' => [
'type' => 'danger',
'message' => 'Une erreur est survenue lors de la suppression du produit.'
]
]; ];
} }


\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;


$productOrder = ProductOrder::searchOne([ $productOrder = ProductOrder::searchOne([
'id' => $idProductOrder
]);
'id' => $idProductOrder
]);


if ($productOrder) { if ($productOrder) {
$productOrder->delete(); $productOrder->delete();


return [ return [
'return' => 'success',
'alert' => [
'type' => 'danger',
'message' => 'Produit supprimé'
]
'return' => 'success',
'alert' => [
'type' => 'danger',
'message' => 'Produit supprimé'
]
]; ];
} }


return [ return [
'return' => 'error',
'alert' => [
'type' => 'danger',
'message' => 'Une erreur est survenue lors de la suppression du produit.'
]
'return' => 'error',
'alert' => [
'type' => 'danger',
'message' => 'Une erreur est survenue lors de la suppression du produit.'
]
]; ];
} }


$class = $this->getClass(); $class = $this->getClass();


$model = $class::searchOne([ $model = $class::searchOne([
'id' => $id
], [
'orderby' => 'teshtygjhtyt'
]);
'id' => $id
], [
'orderby' => 'teshtygjhtyt'
]);


if ($model) { if ($model) {
return $model; return $model;

+ 1
- 1
common/models/Document.php View File

[['date'], 'safe'], [['date'], 'safe'],
[['comment', 'address', 'tax_calculation_method'], 'string'], [['comment', 'address', 'tax_calculation_method'], 'string'],
[['id_user', 'id_producer'], 'integer'], [['id_user', 'id_producer'], 'integer'],
['is_sent', 'boolean'],
[['is_sent', 'ignore_when_billing'], 'boolean'],
[['name', 'reference', 'status'], 'string', 'max' => 255], [['name', 'reference', 'status'], 'string', 'max' => 255],
[['deliveryNotes'], 'safe'] [['deliveryNotes'], 'safe']
]; ];

+ 26
- 0
console/migrations/m230405_090528_add_field_delivery_note_ignore_when_billing.php View File

<?php

use yii\db\Migration;
use yii\db\Schema;

/**
* Class m230405_090528_add_field_delivery_note_ignore_when_billing
*/
class m230405_090528_add_field_delivery_note_ignore_when_billing extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('delivery_note', 'ignore_when_billing', Schema::TYPE_BOOLEAN.' DEFAULT NULL');
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('delivery_note', 'ignore_when_billing');
}
}

Loading…
Cancel
Save