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

@ -47,14 +47,14 @@ class SellController extends AbstractController
'code' => $code, 'code' => $code,
'money' => $acc['money'] 'money' => $acc['money']
]); ]);
if (!$doc){ if (!$doc) {
$canEdit = false; $canEdit = false;
} }
$year = $entityManager->getRepository(Year::class)->findOneBy([ $year = $entityManager->getRepository(Year::class)->findOneBy([
'bid' => $acc['bid'], 'bid' => $acc['bid'],
'head' => true 'head' => true
]); ]);
if($doc->getYear()->getId() != $year->getId()){ if ($doc->getYear()->getId() != $year->getId()) {
$canEdit = false; $canEdit = false;
} }
@ -221,11 +221,11 @@ class SellController extends AbstractController
]); ]);
$hesabdariRow->setRef($ref); $hesabdariRow->setRef($ref);
$entityManager->persist($hesabdariRow); $entityManager->persist($hesabdariRow);
// ذخیره نوع تخفیف و درصد آن // ذخیره نوع تخفیف و درصد آن
$doc->setDiscountType($params['discountType'] ?? 'fixed'); $doc->setDiscountType($params['discountType'] ?? 'fixed');
if (isset($params['discountPercent'])) { if (isset($params['discountPercent'])) {
$doc->setDiscountPercent((float)$params['discountPercent']); $doc->setDiscountPercent((float) $params['discountPercent']);
} }
} }
$doc->setDes($params['des']); $doc->setDes($params['des']);
@ -720,10 +720,10 @@ class SellController extends AbstractController
$params['printers'] = $params['printers'] ?? false; $params['printers'] = $params['printers'] ?? false;
$params['pdf'] = $params['pdf'] ?? true; $params['pdf'] = $params['pdf'] ?? true;
$params['posPrint'] = $params['posPrint'] ?? false; $params['posPrint'] = $params['posPrint'] ?? false;
// دریافت تنظیمات پیش‌فرض از PrintOptions // دریافت تنظیمات پیش‌فرض از PrintOptions
$printSettings = $entityManager->getRepository(PrintOptions::class)->findOneBy(['bid' => $acc['bid']]); $printSettings = $entityManager->getRepository(PrintOptions::class)->findOneBy(['bid' => $acc['bid']]);
// تنظیم مقادیر پیش‌فرض از تنظیمات ذخیره شده // تنظیم مقادیر پیش‌فرض از تنظیمات ذخیره شده
$defaultOptions = [ $defaultOptions = [
'note' => $printSettings ? $printSettings->isSellNote() : true, 'note' => $printSettings ? $printSettings->isSellNote() : true,
@ -735,7 +735,7 @@ class SellController extends AbstractController
'invoiceIndex' => $printSettings ? $printSettings->isSellInvoiceIndex() : true, 'invoiceIndex' => $printSettings ? $printSettings->isSellInvoiceIndex() : true,
'businessStamp' => $printSettings ? $printSettings->isSellBusinessStamp() : true 'businessStamp' => $printSettings ? $printSettings->isSellBusinessStamp() : true
]; ];
// اولویت با پارامترهای ارسالی است // اولویت با پارامترهای ارسالی است
$printOptions = array_merge($defaultOptions, $params['printOptions'] ?? []); $printOptions = array_merge($defaultOptions, $params['printOptions'] ?? []);
@ -770,7 +770,7 @@ class SellController extends AbstractController
$this->renderView('pdf/printers/sell.html.twig', [ $this->renderView('pdf/printers/sell.html.twig', [
'bid' => $acc['bid'], 'bid' => $acc['bid'],
'doc' => $doc, 'doc' => $doc,
'rows' => array_map(function($row) { 'rows' => array_map(function ($row) {
return [ return [
'commodity' => $row->getCommodity(), 'commodity' => $row->getCommodity(),
'commodityCount' => $row->getCommdityCount(), 'commodityCount' => $row->getCommdityCount(),
@ -802,7 +802,7 @@ class SellController extends AbstractController
$this->renderView('pdf/posPrinters/justSell.html.twig', [ $this->renderView('pdf/posPrinters/justSell.html.twig', [
'bid' => $acc['bid'], 'bid' => $acc['bid'],
'doc' => $doc, 'doc' => $doc,
'rows' => array_map(function($row) { 'rows' => array_map(function ($row) {
return [ return [
'commodity' => $row->getCommodity(), 'commodity' => $row->getCommodity(),
'commodityCount' => $row->getCommdityCount(), 'commodityCount' => $row->getCommdityCount(),
@ -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
} }
// تنظیم اطلاعات اصلی فاکتور // تنظیم اطلاعات اصلی فاکتور
$doc->setDes($params['invoiceDescription']); if (isset($params['invoiceDescription'])) {
$doc->setDate($params['invoiceDate']); $doc->setDes($params['invoiceDescription']);
$doc->setTaxPercent($params['taxPercent'] ?? 0); } else {
$doc->setDes('');
}
if (isset($params['invoiceDate'])) {
$doc->setDate($params['invoiceDate']);
} else {
$doc->setDate($jdate->jdate('Y/n/d', time()));
}
if (isset($params['taxPercent'])) {
$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 {
$totalDiscount = $params['totalDiscount']; if (isset($params['totalDiscount']) && $params['totalDiscount'] > 0) {
$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);
$person = $entityManager->getRepository(Person::class)->findOneBy([ if (!isset($params['customer']) || $params['customer'] == '') {
'bid' => $acc['bid'], $person = $entityManager->getRepository(Person::class)->findOneBy([
'id' => $params['customer'] '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([
'bid' => $acc['bid'],
'id' => $params['customer']
]);
}
if (!$person) { if (!$person) {
throw new \Exception('خریدار یافت نشد'); throw new \Exception('خریدار یافت نشد');
} }
@ -1091,10 +1121,10 @@ class SellController extends AbstractController
$paymentDoc->setDate($params['invoiceDate']); $paymentDoc->setDate($params['invoiceDate']);
$paymentDoc->setDes($payment['description'] ?? 'دریافت وجه فاکتور فروش شماره ' . $doc->getCode()); $paymentDoc->setDes($payment['description'] ?? 'دریافت وجه فاکتور فروش شماره ' . $doc->getCode());
$paymentDoc->setAmount($payment['amount']); $paymentDoc->setAmount($payment['amount']);
// ایجاد ارتباط با فاکتور اصلی // ایجاد ارتباط با فاکتور اصلی
$doc->addRelatedDoc($paymentDoc); $doc->addRelatedDoc($paymentDoc);
// ایجاد سطرهای حسابداری بر اساس نوع پرداخت // ایجاد سطرهای حسابداری بر اساس نوع پرداخت
if ($payment['type'] === 'bank') { if ($payment['type'] === 'bank') {
// دریافت از طریق حساب بانکی // دریافت از طریق حساب بانکی
@ -1191,16 +1221,17 @@ class SellController extends AbstractController
'result' => 1, 'result' => 1,
'message' => 'فاکتور با موفقیت ثبت شد', 'message' => 'فاکتور با موفقیت ثبت شد',
'data' => [ 'data' => [
'id' => $doc->getCode(), 'id' => $doc->getCode(),
'code' => $doc->getCode(), 'code' => $doc->getCode(),
'shortlink' => $doc->getShortlink() 'shortlink' => $doc->getShortlink()
] ]
]); ]);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->json([ return $this->json([
'result' => 0, 'result' => 0,
'message' => $e->getMessage() 'message' => $e->getMessage(),
'data' => $params
]); ]);
} }
} }
@ -1241,7 +1272,7 @@ class SellController extends AbstractController
// دریافت اسناد پرداخت مرتبط // دریافت اسناد پرداخت مرتبط
$relatedDocs = $doc->getRelatedDocs(); $relatedDocs = $doc->getRelatedDocs();
foreach ($relatedDocs as $relatedDoc) { foreach ($relatedDocs as $relatedDoc) {
if ($relatedDoc->getType() === 'sell_receive') { if ($relatedDoc->getType() === 'sell_receive') {
$payment = [ $payment = [
@ -1284,7 +1315,7 @@ class SellController extends AbstractController
$itemDiscountType = $row->getDiscountType() ?? 'fixed'; $itemDiscountType = $row->getDiscountType() ?? 'fixed';
$itemDiscountPercent = $row->getDiscountPercent() ?? 0; $itemDiscountPercent = $row->getDiscountPercent() ?? 0;
$itemTax = $row->getTax() ?? 0; $itemTax = $row->getTax() ?? 0;
// محاسبه قیمت واحد و تخفیف // محاسبه قیمت واحد و تخفیف
if ($itemDiscountType === 'percent' && $itemDiscountPercent > 0) { if ($itemDiscountType === 'percent' && $itemDiscountPercent > 0) {
// محاسبه قیمت اصلی در حالت تخفیف درصدی // محاسبه قیمت اصلی در حالت تخفیف درصدی
@ -1294,14 +1325,14 @@ class SellController extends AbstractController
// محاسبه قیمت اصلی در حالت تخفیف مقداری // محاسبه قیمت اصلی در حالت تخفیف مقداری
$originalPrice = $basePrice + $itemDiscount; $originalPrice = $basePrice + $itemDiscount;
} }
// محاسبه قیمت واحد // محاسبه قیمت واحد
$unitPrice = $row->getCommdityCount() > 0 ? $originalPrice / $row->getCommdityCount() : 0; $unitPrice = $row->getCommdityCount() > 0 ? $originalPrice / $row->getCommdityCount() : 0;
// محاسبه قیمت خالص (بدون مالیات) // محاسبه قیمت خالص (بدون مالیات)
$netPrice = $basePrice; $netPrice = $basePrice;
$totalInvoice += $netPrice; $totalInvoice += $netPrice;
$items[] = [ $items[] = [
'name' => [ 'name' => [
'id' => $row->getCommodity()->getId(), 'id' => $row->getCommodity()->getId(),

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 }}