From 89ffdb9aeac5871ec2bd4c5744cd0f1b360d9719 Mon Sep 17 00:00:00 2001 From: babak alizadeh Date: Wed, 23 Oct 2024 22:22:39 +0330 Subject: [PATCH] progress in calculate profit of invoices --- .../src/Controller/BusinessController.php | 13 ++++++++++ hesabixCore/src/Controller/SellController.php | 26 ++++++++++++++++--- hesabixCore/src/Entity/Business.php | 15 +++++++++++ .../src/Repository/HesabdariRowRepository.php | 15 +---------- 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/hesabixCore/src/Controller/BusinessController.php b/hesabixCore/src/Controller/BusinessController.php index 5045d9b..45ee8d7 100644 --- a/hesabixCore/src/Controller/BusinessController.php +++ b/hesabixCore/src/Controller/BusinessController.php @@ -130,6 +130,11 @@ class BusinessController extends AbstractController $response['walletMatchBank'] = null; $response['updateSellPrice'] = $bus->isCommodityUpdateSellPriceAuto(); $response['updateBuyPrice'] = $bus->isCommodityUpdateBuyPriceAuto(); + if (!$bus->getProfitCalctype()) { + $response['profitCalcType'] = 'lis'; + } else { + $response['profitCalcType'] = $bus->getProfitCalctype(); + } if ($bus->isWalletEnable()) $response['walletMatchBank'] = $provider->Entity2Array($bus->getWalletMatchBank(), 0); $year = $entityManager->getRepository(Year::class)->findOneBy([ @@ -235,6 +240,14 @@ class BusinessController extends AbstractController $business->setCommodityUpdateSellPriceAuto(false); } } + if (array_key_exists('profitCalcType', $params)) { + if ($params['profitCalcType'] == 'lis' || $params['profitCalcType'] == 'avgis') { + $business->setProfitCalcType($params['profitCalcType']); + } + } + else{ + $business->setProfitCalcType('lis'); + } $business->setCommodityUpdateSellPriceAuto($params['commodityUpdateSellPriceAuto']); if (array_key_exists('walletEnabled', $params)) { if ($params['walletEnabled']) { diff --git a/hesabixCore/src/Controller/SellController.php b/hesabixCore/src/Controller/SellController.php index 6f14b4b..52a19f6 100644 --- a/hesabixCore/src/Controller/SellController.php +++ b/hesabixCore/src/Controller/SellController.php @@ -70,7 +70,7 @@ class SellController extends AbstractController } #[Route('/api/sell/mod', name: 'app_sell_mod')] - public function app_sell_mod(registryMGR $registryMGR,PluginService $pluginService,SMS $SMS,Provider $provider, Extractor $extractor, Request $request, Access $access, Log $log, EntityManagerInterface $entityManager): JsonResponse + public function app_sell_mod(registryMGR $registryMGR, PluginService $pluginService, SMS $SMS, Provider $provider, Extractor $extractor, Request $request, Access $access, Log $log, EntityManagerInterface $entityManager): JsonResponse { $params = []; if ($content = $request->getContent()) { @@ -214,7 +214,7 @@ class SellController extends AbstractController return $this->json([ 'result' => $SMS->sendByBalance( - [$person->getnikename(), 'sell/' . $acc['bid']->getId() . '/' . $doc->getShortLink() , $acc['bid']->getName(), $acc['bid']->getTel()], + [$person->getnikename(), 'sell/' . $acc['bid']->getId() . '/' . $doc->getShortLink(), $acc['bid']->getName(), $acc['bid']->getTel()], $registryMGR->get('sms', 'plugAccproSharefaktor'), $person->getMobile(), $acc['bid'], @@ -343,13 +343,33 @@ class SellController extends AbstractController $pays += $relatedDoc->getAmount(); } $temp['relatedDocsPays'] = $pays; - + // this variable is for store profit of invoice + $temp['profit'] = 0; foreach ($item->getHesabdariRows() as $item) { if ($item->getRef()->getCode() == '104') { $temp['discountAll'] = $item->getBd(); } elseif ($item->getRef()->getCode() == '61') { $temp['transferCost'] = $item->getBs(); } + + //calculate profit + if ($item->getCommodity()) { + if ($acc['bid']->getProfitCalctype() == 'lis') { + $last = $entityManager->getRepository(HesabdariRow::class)->findOneBy([ + 'commodity' => $item->getCommodity(), + 'bs' => 0 + ], [ + 'id' => 'DESC' + ]); + if ($last) { + $price = $last->getBd() / $last->getCommdityCount(); + $temp['profit'] = $temp['profit'] + (($item->getBs() / $last->getCommdityCount()) - $price); + } + } else { + + } + } + } if (!array_key_exists('discountAll', $temp)) $temp['discountAll'] = 0; diff --git a/hesabixCore/src/Entity/Business.php b/hesabixCore/src/Entity/Business.php index ab8055c..5867be8 100644 --- a/hesabixCore/src/Entity/Business.php +++ b/hesabixCore/src/Entity/Business.php @@ -243,6 +243,9 @@ class Business #[ORM\Column(nullable: true)] private ?bool $CommodityUpdateBuyPriceAuto = null; + #[ORM\Column(length: 30, nullable: true)] + private ?string $profitCalcType = null; + public function __construct() { $this->logs = new ArrayCollection(); @@ -1735,4 +1738,16 @@ class Business return $this; } + + public function getProfitCalcType(): ?string + { + return $this->profitCalcType; + } + + public function setProfitCalcType(?string $profitCalcType): static + { + $this->profitCalcType = $profitCalcType; + + return $this; + } } diff --git a/hesabixCore/src/Repository/HesabdariRowRepository.php b/hesabixCore/src/Repository/HesabdariRowRepository.php index 0242b01..bf8dbbb 100644 --- a/hesabixCore/src/Repository/HesabdariRowRepository.php +++ b/hesabixCore/src/Repository/HesabdariRowRepository.php @@ -2,6 +2,7 @@ namespace App\Repository; +use App\Entity\Commodity; use App\Entity\HesabdariRow; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\NonUniqueResultException; @@ -40,20 +41,6 @@ class HesabdariRowRepository extends ServiceEntityRepository } } -// /** -// * @return HesabdariRow[] Returns an array of HesabdariRow objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('h') -// ->andWhere('h.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('h.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } /** * @throws NonUniqueResultException