bug fix devesion by zero

This commit is contained in:
Hesabix 2025-06-30 08:54:56 +00:00
parent 83baf8ffb4
commit 2f38d5dc6b
2 changed files with 77 additions and 36 deletions

View file

@ -877,7 +877,8 @@ class SellController extends AbstractController
Access $access, Access $access,
Log $log, Log $log,
EntityManagerInterface $entityManager, EntityManagerInterface $entityManager,
registryMGR $registryMGR registryMGR $registryMGR,
Jdate $jdate
): JsonResponse { ): JsonResponse {
$params = []; $params = [];
if ($content = $request->getContent()) { if ($content = $request->getContent()) {
@ -941,12 +942,22 @@ class SellController extends AbstractController
} }
// تنظیم اطلاعات اصلی فاکتور // تنظیم اطلاعات اصلی فاکتور
if (isset($params['invoiceDescription'])) {
$doc->setDes($params['invoiceDescription']); $doc->setDes($params['invoiceDescription']);
} else {
$doc->setDes('');
}
if (isset($params['invoiceDate'])) {
$doc->setDate($params['invoiceDate']); $doc->setDate($params['invoiceDate']);
} else {
$doc->setDate($jdate->jdate('Y/n/d', time()));
}
if (isset($params['taxPercent'])) {
$doc->setTaxPercent($params['taxPercent'] ?? 0); $doc->setTaxPercent($params['taxPercent'] ?? 0);
}
// افزودن هزینه حمل // افزودن هزینه حمل
if ($params['shippingCost'] > 0) { if (isset($params['shippingCost']) && $params['shippingCost'] > 0) {
$hesabdariRow = new HesabdariRow(); $hesabdariRow = new HesabdariRow();
$hesabdariRow->setDes('حمل و نقل کالا'); $hesabdariRow->setDes('حمل و نقل کالا');
$hesabdariRow->setBid($acc['bid']); $hesabdariRow->setBid($acc['bid']);
@ -961,17 +972,21 @@ class SellController extends AbstractController
// افزودن تخفیف کلی // افزودن تخفیف کلی
$totalDiscount = 0; $totalDiscount = 0;
if ($params['discountType'] === 'percent') { if (isset($params['discountType']) && $params['discountType'] === 'percent') {
$totalDiscount = round(($params['totalInvoice'] * $params['discountPercent']) / 100); $totalDiscount = round(($params['totalInvoice'] * $params['discountPercent']) / 100);
$doc->setDiscountType('percent'); $doc->setDiscountType('percent');
$doc->setDiscountPercent((float) $params['discountPercent']); $doc->setDiscountPercent((float) $params['discountPercent']);
} else { } else {
if (isset($params['totalDiscount']) && $params['totalDiscount'] > 0) {
$totalDiscount = $params['totalDiscount']; $totalDiscount = $params['totalDiscount'];
} else {
$totalDiscount = 0;
}
$doc->setDiscountType('fixed'); $doc->setDiscountType('fixed');
$doc->setDiscountPercent(null); $doc->setDiscountPercent(null);
} }
if ($totalDiscount > 0) { if (isset($totalDiscount) && $totalDiscount > 0) {
$hesabdariRow = new HesabdariRow(); $hesabdariRow = new HesabdariRow();
$hesabdariRow->setDes('تخفیف فاکتور'); $hesabdariRow->setDes('تخفیف فاکتور');
$hesabdariRow->setBid($acc['bid']); $hesabdariRow->setBid($acc['bid']);
@ -1055,10 +1070,25 @@ class SellController extends AbstractController
$ref = $entityManager->getRepository(HesabdariTable::class)->findOneBy(['code' => '3']); $ref = $entityManager->getRepository(HesabdariTable::class)->findOneBy(['code' => '3']);
$hesabdariRow->setRef($ref); $hesabdariRow->setRef($ref);
if (!isset($params['customer']) || $params['customer'] == '') {
$person = $entityManager->getRepository(Person::class)->findOneBy([
'bid' => $acc['bid'],
'nikename' => 'مشتری پیش فرض'
]);
if (!$person) {
$person = new Person();
$person->setBid($acc['bid']);
$person->setNikename('مشتری پیش فرض');
$person->setCode($provider->getAccountingCode($acc['bid'], 'person'));
$entityManager->persist($person);
}
} else {
$person = $entityManager->getRepository(Person::class)->findOneBy([ $person = $entityManager->getRepository(Person::class)->findOneBy([
'bid' => $acc['bid'], 'bid' => $acc['bid'],
'id' => $params['customer'] 'id' => $params['customer']
]); ]);
}
if (!$person) { if (!$person) {
throw new \Exception('خریدار یافت نشد'); throw new \Exception('خریدار یافت نشد');
} }
@ -1200,7 +1230,8 @@ class SellController extends AbstractController
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->json([ return $this->json([
'result' => 0, 'result' => 0,
'message' => $e->getMessage() 'message' => $e->getMessage(),
'data' => $params
]); ]);
} }
} }

View file

@ -230,7 +230,12 @@
<td class="center item"> <td class="center item">
{% if item.commodityCount > 0 %} {% if item.commodityCount > 0 %}
{% if item.showPercentDiscount %} {% if item.showPercentDiscount %}
{% set originalPrice = item.bs / (1 - (item.discountPercent / 100)) %} {% set discountDivisor = 1 - (item.discountPercent / 100) %}
{% if discountDivisor <= 0 %}
{% set originalPrice = item.bs %}
{% else %}
{% set originalPrice = item.bs / discountDivisor %}
{% endif %}
{% set unitPrice = originalPrice / item.commodityCount %} {% set unitPrice = originalPrice / item.commodityCount %}
{% else %} {% else %}
{% set originalPrice = item.bs + item.discount %} {% set originalPrice = item.bs + item.discount %}
@ -252,7 +257,12 @@
</td> </td>
<td class="center item"> <td class="center item">
{% if item.showPercentDiscount %} {% if item.showPercentDiscount %}
{% set originalPrice = item.bs / (1 - (item.discountPercent / 100)) %} {% set discountDivisor = 1 - (item.discountPercent / 100) %}
{% if discountDivisor <= 0 %}
{% set originalPrice = item.bs %}
{% else %}
{% set originalPrice = item.bs / discountDivisor %}
{% endif %}
{{ originalPrice|round|number_format }} {{ doc.money.shortName }} {{ originalPrice|round|number_format }} {{ doc.money.shortName }}
{% else %} {% else %}
{{ (item.bs + item.discount)|number_format }} {{ doc.money.shortName }} {{ (item.bs + item.discount)|number_format }} {{ doc.money.shortName }}