src/Service/DonationService.php line 630

Open in your IDE?
  1. <?php
  2. // src/Service/UserService.php
  3. namespace App\Service;
  4. use App\Entity\Donation;
  5. use App\Entity\FilmProject;
  6. use App\Entity\OrderDetails;
  7. use App\Entity\RecurringDonation;
  8. use App\Entity\User;
  9. use App\Repository\ProductRepository;
  10. use App\Service\Xero\ContactService;
  11. use App\Service\Xero\InvoiceService;
  12. use App\String\Constant;
  13. use App\Utility\PdfExporter;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Knp\Snappy\Pdf;
  16. use Twig\Environment;
  17. class DonationService
  18. {
  19.     private OrderDetailsService $orderDetailsService;
  20.     private SalesforceService $salesforceService;
  21.     private EntityManagerInterface $em;
  22.     private MerchantWarriorService $mwService;
  23.     private ContactService $xeroContactService;
  24.     private InvoiceService $xeroInvoiceService;
  25.     private MailService $mailService;
  26.     private UserService $userService;
  27.     private SettingService $settingService;
  28.     private WordpressService $wpService;
  29.     private Environment $twig;
  30.     private Pdf $pdf;
  31.     public function __construct(OrderDetailsService $orderDetailsServiceSalesforceService $salesforceServiceEntityManagerInterface $emMerchantWarriorService $mwServiceContactService $xeroContactServiceInvoiceService $xeroInvoiceServiceMailService $mailServiceUserService $userServiceSettingService $settingServiceWordpressService $wpServiceEnvironment $twigPdf $pdf)
  32.     {
  33.         $this->orderDetailsService $orderDetailsService;
  34.         $this->salesforceService $salesforceService;
  35.         $this->em $em;
  36.         $this->xeroContactService $xeroContactService;
  37.         $this->xeroInvoiceService $xeroInvoiceService;
  38.         $this->mwService $mwService;
  39.         $this->mailService $mailService;
  40.         $this->userService $userService;
  41.         $this->settingService $settingService;
  42.         $this->wpService $wpService;
  43.         $this->twig $twig;
  44.         $this->pdf $pdf;
  45.     }
  46.     /**
  47.      * Create donation object
  48.      *
  49.      * @param User $user
  50.      * @return Donation
  51.      */
  52.     public function createObject(?User $userFilmProject $filmProject null$amount null, array $recurringDetails null$supportdaf 0)
  53.     {
  54.         $currentDatetime = new \DateTime('now');
  55.         $orderDetails $this->orderDetailsService->createDonationOrderObj($user$supportdaf);
  56.         $donation = (new Donation())
  57.             ->setCreatedAt($currentDatetime)
  58.             ->setModifiedAt($currentDatetime)
  59.             ->setOrderDetails($orderDetails)
  60.         ;
  61.         if ($filmProject) {
  62.             $donation->setFilmProject($filmProject);
  63.             $orderItems $orderDetails->getOrderItems();
  64.             $orderItems[0]->setProduct($filmProject->getProduct());
  65.             if ($supportdaf 0) {
  66.                 $dafProduct $this->settingService->getDefaultDafDonationProduct();
  67.                 $orderItems[1]->setProduct($dafProduct);
  68.                 $donation->setSupportdaf($supportdaf);
  69.                 $amount $amount $supportdaf;
  70.             }
  71.             // $orderDetails->setFilmProject($filmProject);
  72.             $orderDetails->setTotal($amount);
  73.         }
  74.         if (!empty($recurringDetails)) {
  75.             $frequency $recurringDetails['frequency'];
  76.             $totalPeriod $recurringDetails['totalPeriod'];
  77.             $totalPaymentCount $this->getTotalPaymentCount($frequency$totalPeriod);
  78.             $recurringDonation = (new RecurringDonation)
  79.                 ->setFrequency($frequency)
  80.                 ->setCurrentPaymentCount(0)
  81.                 ->setTotalPaymentCount($totalPaymentCount)
  82.             ;
  83.             $donation
  84.                 ->setRecurringDonation($recurringDonation)
  85.                 ->setIsRecurringDonation(true)
  86.             ;
  87.         }
  88.         return $donation;
  89.     }
  90.     /**
  91.      * Event after persist donation object
  92.      *
  93.      * @param Donation $entityInstance
  94.      * @return void
  95.      */
  96.     public function dispatchPersistEntity($entityInstance$isDafSupport false)
  97.     {
  98.         // Enable donation email if it's null
  99.         if (empty($entityInstance->isIsDonationEmail()) && $entityInstance->isIsDonationEmail() !== false) {
  100.             $entityInstance->setIsDonationEmail(true);
  101.         }
  102.         $this->em->persist($entityInstance);
  103.         $this->em->flush();
  104.         $orderDetails $entityInstance->getOrderDetails();
  105.         $user $orderDetails->getUser();
  106.         if (empty($orderDetails->getXeroId())) {
  107.             $this->createInvoice($entityInstance$user); // XERO process here
  108.         }
  109.         if ($isDafSupport) { // IF support-our-work
  110.             if (empty($orderDetails->getSalesforceId())) {
  111.                 $this->createDafSalesforceObject($entityInstancetrue);
  112.             }
  113.             if ($entityInstance->isIsDonationEmail()) {
  114.                 $this->sendDafDonationEmail($entityInstance);
  115.             }
  116.         } else { // ELSE default donation
  117.             $filmProject $entityInstance->getFilmProject();
  118.             if (empty($orderDetails->getSalesforceId())) {
  119.                 $this->createSalesforceObject($entityInstance);
  120.             }
  121.             
  122.             if ($orderDetails->getUser()) {
  123.                 $filmProject->addUser($orderDetails->getUser());
  124.             }
  125.             $filmProject->addTotalDonation($orderDetails->getTotal()); // Update total donation on selected film project
  126.             $this->em->persist($filmProject);
  127.             $this->em->persist($entityInstance);
  128.             $this->em->flush();
  129.             if ($entityInstance->isIsDonationEmail()) { // Sends email first
  130.                 $this->sendEmail($entityInstance);
  131.             }
  132.             $this->createWordpressObject($entityInstance);
  133.         }
  134.     }
  135.     /**
  136.      * create empty daf donation obj function
  137.      *
  138.      * @param [type] $user
  139.      * @param integer $supportdaf
  140.      * @param array|null $recurringDetails
  141.      * @return Donation
  142.      */
  143.     public function createDafDonationObject($user$supportdaf 0, array $recurringDetails nullFilmProject $supportProject null): Donation
  144.     {
  145.         $currentDatetime = new \DateTime('now');
  146.         $orderDetails $this->orderDetailsService->createDafDonationOrderObj($user$supportdaf);
  147.         $donation = (new Donation())
  148.             ->setCreatedAt($currentDatetime)
  149.             ->setModifiedAt($currentDatetime)
  150.             ->setOrderDetails($orderDetails)
  151.         ;
  152.         if ($supportdaf 0) {
  153.             $dafProduct $this->settingService->getDefaultDafDonationProduct(); // Set product
  154.             if ($supportProject) {
  155.                 $donation->setFilmProject($supportProject);
  156.                 $dafProduct $supportProject->getProduct();
  157.             }
  158.             if (!empty($recurringDetails) && !empty($supportProject->getRecurringProduct())) { // If recurring donation, use recurring Product
  159.                 $dafProduct $supportProject->getRecurringProduct();
  160.             }
  161.             
  162.             $orderItems $orderDetails->getOrderItems();
  163.             $orderItems[0]->setProduct($dafProduct);
  164.             // NOTE: remove supportdaf to fix recurring donation created twice on Salesforce
  165.             // $donation->setSupportdaf($supportdaf);
  166.             $orderDetails->setTotal($supportdaf);
  167.         }
  168.         if (!empty($recurringDetails)) {
  169.             $frequency $recurringDetails['frequency'];
  170.             $totalPeriod $recurringDetails['totalPeriod'];
  171.             $totalPaymentCount $this->getTotalPaymentCount($frequency$totalPeriod);
  172.             $recurringDonation = (new RecurringDonation)
  173.                 ->setFrequency($frequency)
  174.                 ->setCurrentPaymentCount(0)
  175.                 ->setTotalPaymentCount($totalPaymentCount)
  176.             ;
  177.             $donation
  178.                 ->setRecurringDonation($recurringDonation)
  179.                 ->setIsRecurringDonation(true)
  180.             ;
  181.         }
  182.         return $donation;
  183.     }
  184.     /**
  185.      * Create Invoice from order details
  186.      *
  187.      * @param Donation $entityInstance
  188.      * @param User|null $user
  189.      * @return void
  190.      */
  191.     protected function createInvoice(Donation $entityInstanceUser $user null)
  192.     {
  193.         $orderDetails $entityInstance->getOrderDetails();
  194.         $orderBillingDetails $orderDetails->getOrderBillingDetails();
  195.         $xeroId null;
  196.         if ($entityInstance->isIsDisplayOrganisation() && $orderBillingDetails->getOrganisation() != "") {
  197.             $xeroId $this->processOrganisationContact($orderDetails$orderBillingDetails);
  198.         } else {
  199.             $xeroId $this->processPrimaryContact($orderDetails);
  200.         }
  201.         
  202.         $orderDetails $this->xeroInvoiceService->createDonationInvoice($entityInstance$xeroId);
  203.         if ($xeroId) {
  204.             $orderDetails->setXeroContactId($xeroId);
  205.         }
  206.         $this->em->persist($orderDetails);
  207.         $this->em->flush();
  208.     }
  209.     /**
  210.      * Processes Organisation Contact on Xero
  211.      *
  212.      * @param OrderDetails $orderDetails
  213.      * @param OrderBillingDetails $orderBillingDetails
  214.      * @return void
  215.      */
  216.     private function processOrganisationContact($orderDetails$orderBillingDetails) {
  217.         $xeroId null;
  218.         $organisation $orderBillingDetails->getOrganisation();
  219.         if (!empty($organisation)) {
  220.             $xeroId $this->xeroContactService->getContactOrganisationByOrderDetails($orderDetails);
  221.         } 
  222.         if (empty($xeroId)) {
  223.             $xeroId $this->xeroContactService->createContactOrganisationByOrderDetails($orderDetails);
  224.         } else {
  225.             // Update the organisation with newly additional person
  226.             $this->xeroContactService->updateOrganisationWithAdditionalPersonByOrderDetails($orderDetails$xeroId);
  227.         }
  228.         return $xeroId;
  229.     }
  230.     /**
  231.      * Processes Primary Contact on Xero
  232.      *
  233.      * @param OrderDetails $orderDetails
  234.      * @return void
  235.      */
  236.     private function processPrimaryContact($orderDetails) {
  237.         $xeroId $this->xeroContactService->getContactByOrderDetails($orderDetails);
  238.         if (empty($xeroId)) {
  239.             $xeroId $this->xeroContactService->createContactByOrderDetails($orderDetails);
  240.         }
  241.         return $xeroId;
  242.     }
  243.     public function createSalesforceObject(Donation $entityInstance$isDafSupport false)
  244.     {
  245.         // $popupDonationId = '701Bn0000095BSLIA2'; // Staging site
  246.         $popupDonationId '7010K000001eqWhQAI'// Production
  247.         if ($entityInstance->getSupportdaf()) {
  248.             $id $this->salesforceService->createDafDonationTransaction($entityInstance$popupDonationId);
  249.             if ($id) {
  250.                 $entityInstance->setSupportDafSalesforce($id);
  251.                 $this->em->persist($entityInstance);
  252.                 $this->em->flush();
  253.             }
  254.         } 
  255.             
  256.         $id $this->salesforceService->createDonationTransaction($entityInstance);
  257.         if ($id) {
  258.             $orderDetails $entityInstance->getOrderDetails();
  259.             $orderDetails->setSalesforceId($id);
  260.             $this->em->persist($orderDetails);
  261.             $this->em->flush();
  262.         }
  263.     }
  264.     protected function createDafSalesforceObject($entityInstance$isDafSupport false)
  265.     {
  266.         $id $this->salesforceService->createDafDonationTransaction($entityInstance);
  267.         
  268.         if ($id) {
  269.             $orderDetails $entityInstance->getOrderDetails();
  270.             $orderDetails->setSalesforceId($id);
  271.             $this->em->persist($orderDetails);
  272.             $this->em->flush();
  273.         }
  274.     }
  275.     public function sendInvoice(Donation $entityInstance) {
  276.         $this->sendEmail($entityInstance);
  277.     }
  278.     protected function sendEmail($entityInstance)
  279.     {
  280.         $orderDetails $entityInstance->getOrderDetails();
  281.         // $attachmentPath = $this->xeroInvoiceService->getInvoiceAsPdf($orderDetails->getXeroId());
  282.         $pdfExporter = new PdfExporter($this->twig$this->pdf);
  283.         $orderDetails->setDonation($entityInstance);
  284.         $attachmentPath $pdfExporter->convertDonationReceiptPdf($orderDetailstruetrue);
  285.         $this->mailService->sendDonationEmail(Constant::MAIL_DONATION_RECEIVED$entityInstance$attachmentPath);
  286.     }
  287.     protected function sendDafDonationEmail($entityInstance)
  288.     {
  289.         $orderDetails $entityInstance->getOrderDetails();
  290.         $pdfExporter = new PdfExporter($this->twig$this->pdf);
  291.         $orderDetails->setDonation($entityInstance);
  292.         $attachmentPath $pdfExporter->convertDonationReceiptPdf($orderDetailstruetrue);
  293.         // $attachmentPath = $this->xeroInvoiceService->getInvoiceAsPdf($orderDetails->getXeroId());
  294.         $this->mailService->sendDonationEmail(Constant::MAIL_DAF_DONATION_RECEIVED$entityInstance$attachmentPath);
  295.     }
  296.     public function createWordpressObject(Donation $entityInstance)
  297.     {
  298.         $this->wpService->updateFilmObjectDonation($entityInstance);
  299.     }
  300.     /**
  301.      * Proceed payment event for donation
  302.      *
  303.      * @param Donation $entityInstance
  304.      * @param [type] $usedCardID
  305.      * @param [type] $isAddCard
  306.      * @return void
  307.      */
  308.     public function proceedPayment(Donation $entityInstance$usedCardID null$isAddCard null)
  309.     {
  310.         $this->em->persist($entityInstance); // need to save details first before getting dispatched
  311.         $this->em->flush();
  312.         $orderDetails $entityInstance->getOrderDetails();
  313.         
  314.         if ($usedCardID && $usedCardID != '') { // If use previous credit card
  315.             $this->mwService->processCardByCardId($orderDetails$usedCardID);
  316.         } else {
  317.             if (!$isAddCard) {
  318.                 $isAddCard 0;
  319.             }
  320.             $orderDetails $this->mwService->processCard($orderDetails$isAddCardfalse);
  321.             $orderBillingDetails $orderDetails->getOrderBillingDetails();
  322.             if (!empty($orderBillingDetails->getCardId())){ // Save credit card to DB
  323.                 $user $orderDetails->getUser();
  324.                 $this->userService->saveUserCreditCard($user$orderBillingDetails->getCardId());
  325.                 $this->orderDetailsService->addCardIDtoBillingOrder($orderDetails$orderBillingDetails->getCardId());
  326.             }
  327.         }
  328.         $paymentDetails $orderDetails->getPaymentDetails();
  329.         $paymentDetails->setPaid();
  330.         $this->updateOrderDetails($orderDetails);
  331.     }
  332.     /**
  333.      * Update recurring donation
  334.      *
  335.      * @param Donation $entityInstance
  336.      * @return Donation
  337.      */
  338.     public function updateRecurringDonation(Donation $entityInstance): Donation
  339.     {
  340.         $recurringDonation $entityInstance->getRecurringDonation();
  341.         $newPaymentcount $recurringDonation->getCurrentPaymentCount() + 1;
  342.         $recurringDonation->setCurrentPaymentCount($newPaymentcount);
  343.         $frequency $recurringDonation->getFrequency();
  344.         $nextDatetime = new \DateTime('today');
  345.         switch($frequency) {
  346.             case 'weekly':
  347.                 $nextDatetime $nextDatetime->modify('+ 1week');
  348.                 break;
  349.             case 'monthly':
  350.                 $nextDatetime $nextDatetime->modify('+ 1month');
  351.                 break;
  352.         }
  353.         $orderDetails $entityInstance->getOrderDetails();
  354.         $orderBillingDetails $orderDetails->getOrderBillingDetails();
  355.         if ($newPaymentcount == 1) { // Means this is the start date of the payment
  356.             $recurringDonation->setStartPaymentDate($nextDatetime);
  357.         }
  358.         $recurringDonation->setNextPaymentDate($nextDatetime);
  359.         if ($orderBillingDetails->getCardId()) {
  360.             $recurringDonation->setCardId($orderBillingDetails->getCardId());
  361.         }
  362.         $this->em->persist($recurringDonation);
  363.         $this->em->flush();
  364.         return $entityInstance;
  365.     }
  366.     public function updateRecurringDonationTimeline(Donation $entityInstance): Donation
  367.     {
  368.         $recurringDonation $entityInstance->getRecurringDonation();
  369.         $newPaymentcount $recurringDonation->getCurrentPaymentCount() + 1;
  370.         $recurringDonation->setCurrentPaymentCount($newPaymentcount);
  371.         $frequency $recurringDonation->getFrequency();
  372.         $nextDatetime = new \DateTime('today');
  373.         switch($frequency) {
  374.             case 'weekly':
  375.                 $nextDatetime $nextDatetime->modify('+ 1week');
  376.                 break;
  377.             case 'monthly':
  378.                 $nextDatetime $nextDatetime->modify('+ 1month');
  379.                 break;
  380.         }
  381.         if ($newPaymentcount == 1) { // Means this is the start date of the payment
  382.             $recurringDonation->setStartPaymentDate(new \DateTime('today'));
  383.         }
  384.         $recurringDonation->setNextPaymentDate($nextDatetime);
  385.         $this->em->persist($recurringDonation);
  386.         $this->em->flush();
  387.         return $entityInstance;
  388.     }
  389.     /**
  390.      * Get total payment count based on parameters given
  391.      *
  392.      * @param string $frequency
  393.      * @param integer $period
  394.      * @return int
  395.      */
  396.     protected function getTotalPaymentCount(string $frequencyint $period): int
  397.     {
  398.         $totalPaymentCount 0;
  399.         switch($frequency){
  400.             case 'weekly':
  401.                 $totalPaymentCount $period 4;
  402.                 break;
  403.             case 'monthly':
  404.                 $totalPaymentCount $period 1
  405.                 break;
  406.         }
  407.         return $totalPaymentCount;
  408.     }
  409.     /**
  410.      * Clone donation with order details from existing recurring donation
  411.      *
  412.      * @param Donation $donation
  413.      * @return Donation
  414.      */
  415.     public function recreateDonationForRecurringDonation(Donation $donation): Donation
  416.     {
  417.         $orderDetails $donation->getOrderDetails();
  418.         // Order billing details
  419.         $newPaymentDetails = clone $orderDetails->getPaymentDetails();
  420.         $this->em->persist($newPaymentDetails);
  421.         $newOrderBillingDetails =  clone $orderDetails->getOrderBillingDetails();
  422.         $this->em->persist($newOrderBillingDetails);
  423.         $newOrderDetails = clone $donation->getOrderDetails();
  424.         $newOrderDetails
  425.             ->setOrderBillingDetails($newOrderBillingDetails)
  426.             ->setPaymentDetails($newPaymentDetails)
  427.             ->setCreatedAt(new \DateTime('now'))
  428.             ->setModifiedAt(new \DateTime('now'))
  429.             ->setSalesforceId(null)
  430.         ;
  431.         $this->em->persist($newOrderDetails);
  432.         $newDonation = clone $donation;
  433.         $newDonation->setOrderDetails($newOrderDetails);
  434.         $newDonation $this->updateNewDonationObj($newDonation);
  435.         $this->em->persist($newDonation);
  436.         $this->em->flush();
  437.         return $newDonation;
  438.     }
  439.     public function refundDonation(Donation $donation)
  440.     {
  441.         $orderDetails $donation->getOrderDetails();
  442.         $this->mwService->refund($orderDetails); // Merchant Warrior
  443.         $this->salesforceService->refundTransaction($orderDetails); // Salesforce update
  444.         if ($donation->getSupportDafSalesforce()) {
  445.             $this->salesforceService->refundDafSupportTransaction($donation); // Salesforce popup update
  446.         }
  447.         
  448.         $xeroContactId $orderDetails->getXeroContactId(); // Xero Refund update
  449.         if (!empty($xeroContactId)) $this->xeroInvoiceService->createDonationRefundInvoice($donation$xeroContactId);
  450.         $this->updateOrderDetails($orderDetails);
  451.         $filmProject $donation->getFilmProject();
  452.             if ($filmProject) {
  453.                 $donationRepository $this->em->getRepository(Donation::class);
  454.                 $donations $donationRepository->findDonationsByProjectOrderDate($filmProject->getId());
  455.                 $totalDonation 0;
  456.                 foreach ($donations as $currentDonation) {
  457.                     $currentOrderDetails $currentDonation->getOrderDetails();
  458.                     
  459.                     $currentAmount $currentOrderDetails->getTotal();
  460.                     // Need to exclude popup amount if there is one
  461.                     $supportdaf $currentDonation->getSupportDaf();
  462.                     if ($supportdaf) { 
  463.                         $currentAmount -= $supportdaf;
  464.                     }
  465.     
  466.                     $totalDonation += $currentAmount;
  467.                 }
  468.                 $filmProject->setTotalDonation($totalDonation);
  469.                 $this->em->persist($filmProject);
  470.                 $this->em->flush();
  471.             
  472.             if (!$filmProject->isIsDafCore()) $this->wpService->updateFilmObjectDonation($donation); // Finally update wordpress end
  473.         }
  474.     }
  475.     public function partialRefundDonation(Donation $donation)
  476.     {
  477.         $refundAmount =  '' number_format(((float)$donation->getSupportdaf() / 100), 2'.''');
  478.         $orderDetails $donation->getOrderDetails();
  479.         $this->mwService->partialRefund($orderDetails$refundAmount); // Merchant Warrior
  480.         if ($donation->getSupportDafSalesforce()) {
  481.             $this->salesforceService->refundDafSupportTransaction($donation); // Salesforce update
  482.         }
  483.         if (!empty($xeroContactId)) $this->xeroInvoiceService->createPartialDonationRefundInvoice($donation$xeroContactId);
  484.         $this->updateOrderDetails($orderDetails);
  485.     }
  486.     public function checkRecurringDonationPastCampaign($donation): bool
  487.     {
  488.         return false;
  489.     }
  490.     public function appearAsAnonymous(Donation $donation)
  491.     {
  492.         $donation->setAsAnonymous();
  493.         $this->em->persist($donation);
  494.         $this->em->flush();
  495.         $this->createWordpressObject($donation);
  496.     }
  497.     public function appearAsNonAnonymous(Donation $donation)
  498.     {
  499.         $donation->setAsNonAnonymous();
  500.         $this->em->persist($donation);
  501.         $this->em->flush();
  502.         $this->createWordpressObject($donation);
  503.     }
  504.     public function setAmountVisibility(Donation $donationbool $isInvisible)
  505.     {
  506.         $donation->setIsAmountInvisible($isInvisible);
  507.         $this->em->persist($donation);
  508.         $this->em->flush();
  509.         $this->createWordpressObject($donation);
  510.     }
  511.     protected function updateNewDonationObj(Donation $donation): Donation
  512.     {
  513.         $currentDatetime = new \DateTime('today');
  514.         $donation->setCreatedAt($currentDatetime);
  515.         $donation->setModifiedAt($currentDatetime);
  516.         return $donation;
  517.     }
  518.     protected function updateOrderDetails($orderDetails)
  519.     {
  520.         $orderDetails->setModifiedAt(new \DateTime('now'));
  521.         $this->em->persist($orderDetails);
  522.         $this->em->flush();
  523.     }
  524.     public function updateDonation(Donation $donation$oldProjectId null)
  525.     {
  526.         $donationRepository $this->em->getRepository(Donation::class);
  527.         $orderDetails $donation->getOrderDetails();
  528.         if ($orderDetails->getXeroContactId()) { // Update xero contact details
  529.             $this->xeroContactService->updateContactById($orderDetails$orderDetails->getXeroContactId());
  530.         }
  531.         $amount $orderDetails->getTotal() / 100// Update xero donation amount
  532.         if ($orderDetails->getXeroId()){
  533.             $this->xeroInvoiceService->updateInvoice($donation$orderDetails->getXeroId(), $amount$oldProjectId);
  534.         }
  535.         if ($orderDetails->getSalesforceId()) {
  536.             $this->salesforceService->updateTransaction($orderDetails); // update Salesforce
  537.         } else {
  538.             $id $this->salesforceService->createDonationTransaction($donation);
  539.             if ($id) {
  540.                 $orderDetails->setSalesforceId((string)$id);
  541.                 $this->em->persist($orderDetails);
  542.                 $this->em->flush();
  543.             }
  544.         }
  545.         $this->wpService->updateFilmObjectDonation($donation); // Update total wordpress object
  546.         $filmProject $donation->getFilmProject();
  547.         if ($filmProject) {
  548.             $donations $donationRepository->findDonationsByProjectOrderDate($filmProject->getId());
  549.             $this->updateDonationTotalEntity($donations$filmProject);
  550.         }
  551.         if ($oldProjectId) { // Added workflow to update previous project to remove the donation
  552.             $filmProjectRepository $this->em->getRepository(FilmProject::class);
  553.             $oldProject $filmProjectRepository->findOneBy(['id' => $oldProjectId]);
  554.             if ($oldProject) {
  555.                 $this->wpService->updateFilmObjectDonationByProject($oldProject); // Update previous project page
  556.                 $donations $donationRepository->findDonationsByProjectOrderDate($oldProjectId); // Update project in the system
  557.                 $this->updateDonationTotalEntity($donations$oldProject); 
  558.             }
  559.         }
  560.     }
  561.     public function archiveDonation(Donation $donation)
  562.     {
  563.         $this->wpService->updateFilmObjectDonation($donation); // Update total wordpress object
  564.         
  565.         $filmProject $donation->getFilmProject();
  566.         if ($filmProject) {
  567.             $donationRepository $this->em->getRepository(Donation::class);
  568.             $donations $donationRepository->findDonationsByProjectOrderDate($filmProject->getId());
  569.             $totalDonation 0;
  570.             foreach ($donations as $currentDonation) {
  571.                 $currentOrderDetails $currentDonation->getOrderDetails();
  572.                 $currentAmount $currentOrderDetails->getTotal();
  573.                 // Need to exclude popup amount if there is one
  574.                 $supportdaf $currentDonation->getSupportDaf();
  575.                 if ($supportdaf) { 
  576.                     $currentAmount -= $supportdaf;
  577.                 }
  578.                 $totalDonation += $currentAmount;
  579.             }
  580.             $filmProject->setTotalDonation($totalDonation);
  581.             $this->em->persist($filmProject);
  582.             $this->em->flush();
  583.         }       
  584.     }
  585.     private function updateDonationTotalEntity(array $donationsFilmProject $filmProject) {
  586.         $totalDonation 0;
  587.         foreach ($donations as $currentDonation) {
  588.             $currentOrderDetails $currentDonation->getOrderDetails();
  589.             $totalDonation += $currentOrderDetails->getTotal() / 100;
  590.         }
  591.         $filmProject->setTotalDonation($totalDonation 100);
  592.         $this->em->persist($filmProject);
  593.         $this->em->flush();
  594.     }
  595. }