/**
  * Form builder
  *
  * @param FormBuilderInterface $builder
  * @param array $options
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $builder->add('vatInfo', ChoiceType::class, array('label' => 'SecondaryVat.vatInfo.label', 'choices_as_values' => true, 'choices' => SecondaryVat::choiceVatInfo(), 'attr' => array('choice_label_trans' => true)));
     $builder->add('vat', NumberType::class, array('label' => 'SecondaryVat.vat.label'));
     $builder->add('balanceTtc', NumberType::class, array('label' => 'SecondaryVat.balanceTtc.label'));
     $builder->add('balanceNet', NumberType::class, array('label' => 'SecondaryVat.balanceNet.label'));
 }
Esempio n. 2
0
 /**
  * Form builder
  *
  * @param FormBuilderInterface $builder
  * @param array $options
  *
  * @return null
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $this->sale = $options['sale'];
     if (null == $this->sale) {
         $builder->add('sale', EntityType::class, array('label' => 'SecondatVat.sale.label', 'class' => 'AcfDataBundle:Sale', 'query_builder' => function (SaleRepository $sr) {
             return $sr->createQueryBuilder('s')->orderBy('s.number', 'ASC');
         }, 'choice_label' => 'number', 'multiple' => false, 'by_reference' => true, 'required' => true));
     } else {
         $saleId = $this->sale->getId();
         $builder->add('sale', EntityidType::class, array('label' => 'SecondatVat.sale.label', 'class' => 'AcfDataBundle:Sale', 'query_builder' => function (SaleRepository $sr) use($saleId) {
             return $sr->createQueryBuilder('s')->where('s.id = :id')->setParameter('id', $saleId)->orderBy('s.number', 'ASC');
         }, 'choice_label' => 'id', 'multiple' => false, 'by_reference' => true, 'required' => true));
     }
     $builder->add('vat', NumberType::class, array('label' => 'SecondaryVat.vat.label'));
     $builder->add('balanceTtc', NumberType::class, array('label' => 'SecondaryVat.balanceTtc.label'));
     $builder->add('balanceNet', NumberType::class, array('label' => 'SecondaryVat.balanceNet.label'));
     $builder->add('vatInfo', ChoiceType::class, array('label' => 'SecondaryVat.vatInfo.label', 'choices_as_values' => true, 'choices' => SecondaryVat::choiceVatInfo(), 'attr' => array('choice_label_trans' => true)));
 }
 protected function traceEntity(SecondaryVat $cloneSecondaryVat, SecondaryVat $secondaryVat)
 {
     $curUser = $this->getSecurityTokenStorage()->getToken()->getUser();
     $trace = new Trace();
     $trace->setActionId($secondaryVat->getId());
     $trace->setActionType(Trace::AT_UPDATE);
     $trace->setUserId($curUser->getId());
     $trace->setCompanyId($secondaryVat->getCompany()->getId());
     $trace->setUserFullname($curUser->getFullName());
     if (!$this->hasRole('ROLE_SUPERADMIN')) {
         if (!$this->hasRole('ROLE_ADMIN')) {
             $trace->setUserType(Trace::UT_CLIENT);
         } else {
             $trace->setUserType(Trace::UT_ADMIN);
         }
     } else {
         $trace->setUserType(Trace::UT_SUPERADMIN);
     }
     $tableBegin = ': <br><table class="table table-bordered table-condensed table-hover table-striped">';
     $tableBegin .= '<thead><tr><th class="text-left">' . $this->translate('Entity.field') . '</th>';
     $tableBegin .= '<th class="text-left">' . $this->translate('Entity.oldVal') . '</th>';
     $tableBegin .= '<th class="text-left">' . $this->translate('Entity.newVal') . '</th></tr></thead><tbody>';
     $tableEnd = '</tbody></table>';
     $trace->setActionEntity(Trace::AE_SECONDARYVAT);
     $trace->setActionId2($secondaryVat->getSale()->getMonthlyBalance()->getCompany()->getId());
     $trace->setActionEntity2(Trace::AE_COMPANY);
     $trace->setActionId3($secondaryVat->getSale()->getMonthlyBalance()->getId());
     $trace->setActionEntity2(Trace::AE_MBSALE);
     $trace->setActionId3($secondaryVat->getSale()->getId());
     $trace->setActionEntity2(Trace::AE_SALE);
     $msg = '';
     if ($cloneSecondaryVat->getVatInfo() != $secondaryVat->getVatInfo()) {
         $msg .= '<tr><td>' . $this->translate('SecondaryVat.vatInfo.label') . '</td><td>';
         if ($cloneSecondaryVat->getVatInfo() == null) {
             $msg .= '<span class="label label-warning">' . $this->translate('_NA') . '</span>';
         } else {
             $msg .= $this->translate('SecondaryVat.vatInfo.' . $cloneSecondaryVat->getVatInfo());
         }
         $msg .= '</td><td>';
         if ($secondaryVat->getVatInfo() == null) {
             $msg .= '<span class="label label-warning">' . $this->translate('_NA') . '</span>';
         } else {
             $msg .= $this->translate('SecondaryVat.vatInfo.' . $secondaryVat->getVatInfo());
         }
         $msg .= '</td></tr>';
     }
     if ($cloneSecondaryVat->getBalanceNet() != $secondaryVat->getBalanceNet()) {
         $msg .= '<tr><td>' . $this->translate('SecondaryVat.balanceNet.label') . '</td><td>';
         if ($cloneSecondaryVat->getBalanceNet() == null) {
             $msg .= '<span class="label label-warning">' . $this->translate('_NA') . '</span>';
         } else {
             $msg .= $cloneSecondaryVat->getBalanceNet();
         }
         $msg .= '</td><td>';
         if ($secondaryVat->getBalanceNet() == null) {
             $msg .= '<span class="label label-warning">' . $this->translate('_NA') . '</span>';
         } else {
             $msg .= $secondaryVat->getBalanceNet();
         }
         $msg .= '</td></tr>';
     }
     if ($cloneSecondaryVat->getBalanceTtc() != $secondaryVat->getBalanceTtc()) {
         $msg .= '<tr><td>' . $this->translate('SecondaryVat.balanceTtc.label') . '</td><td>';
         if ($cloneSecondaryVat->getBalanceTtc() == null) {
             $msg .= '<span class="label label-warning">' . $this->translate('_NA') . '</span>';
         } else {
             $msg .= $cloneSecondaryVat->getBalanceTtc();
         }
         $msg .= '</td><td>';
         if ($secondaryVat->getBalanceTtc() == null) {
             $msg .= '<span class="label label-warning">' . $this->translate('_NA') . '</span>';
         } else {
             $msg .= $secondaryVat->getBalanceTtc();
         }
         $msg .= '</td></tr>';
     }
     if ($msg != '') {
         $msg = $tableBegin . $msg . $tableEnd;
         $trace->setMsg($this->translate('SecondaryVat.traceEdit', array('%secondaryVat%' => $this->translate('SecondaryVat.vatInfo.' . $secondaryVat->getVatInfo()), '%sale%' => $secondaryVat->getSale()->getLabel(), '%mbsale%' => $secondaryVat->getSale()->getMonthlyBalance()->getRef(), '%company%' => $secondaryVat->getSale()->getMonthlyBalance()->getCompany()->getCorporateName())) . $msg);
         $trace->setDtCrea(new \DateTime('now'));
         $em = $this->getEntityManager();
         $em->persist($trace);
         $em->flush();
     }
 }
Esempio n. 4
0
 /**
  *
  * @param string $uid
  *
  * @return Response
  *
  */
 public function editPostAction($uid)
 {
     $urlFrom = $this->getReferer();
     if (null == $urlFrom || trim($urlFrom) == '') {
         $urlFrom = $this->generateUrl('_admin_company_list');
     }
     $em = $this->getEntityManager();
     try {
         $mbsale = $em->getRepository('AcfDataBundle:MBSale')->find($uid);
         if (null == $mbsale) {
             $this->flashMsgSession('warning', $this->translate('MBSale.edit.notfound'));
         } else {
             $traces = $em->getRepository('AcfDataBundle:Trace')->getAllByEntityId($mbsale->getId(), Trace::AE_MBSALE);
             $this->gvars['traces'] = array_reverse($traces);
             $mbsaleUpdateCountForm = $this->createForm(MBSaleUpdateCountTForm::class, $mbsale);
             $sale = new Sale();
             $sale->setMonthlyBalance($mbsale);
             $saleNewForm = $this->createForm(SaleNewTForm::class, $sale, array('monthlybalance' => $mbsale));
             $saleImportForm = $this->createForm(SaleImportTForm::class);
             $mbsaleUpdateDocsForm = $this->createForm(MBSaleUpdateDocsTForm::class, $mbsale, array('company' => $mbsale->getCompany()));
             $doc = new Doc();
             $doc->setCompany($mbsale->getCompany());
             $docNewForm = $this->createForm(DocNewTForm::class, $doc, array('company' => $mbsale->getCompany()));
             $this->gvars['tabActive'] = $this->getSession()->get('tabActive', 2);
             $this->getSession()->remove('tabActive');
             $this->gvars['stabActive'] = $this->getSession()->get('stabActive', 1);
             $this->getSession()->remove('stabActive');
             $request = $this->getRequest();
             $reqData = $request->request->all();
             $cloneMBSale = clone $mbsale;
             if (isset($reqData['SaleImportForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $this->gvars['stabActive'] = 1;
                 $this->getSession()->set('stabActive', 1);
                 $saleImportForm->handleRequest($request);
                 if ($saleImportForm->isValid()) {
                     ini_set('memory_limit', '4096M');
                     ini_set('max_execution_time', '0');
                     $extension = $saleImportForm['excel']->getData()->guessExtension();
                     if ($extension == 'zip') {
                         $extension = 'xlsx';
                     }
                     $filename = uniqid() . '.' . $extension;
                     $saleImportForm['excel']->getData()->move($this->getParameter('adapter_files'), $filename);
                     $fullfilename = $this->getParameter('adapter_files');
                     $fullfilename .= '/' . $filename;
                     $excelObj = $this->get('phpexcel')->createPHPExcelObject($fullfilename);
                     $log = '';
                     $iterator = $excelObj->getWorksheetIterator();
                     $activeSheetIndex = -1;
                     $i = 0;
                     foreach ($iterator as $worksheet) {
                         $worksheetTitle = $worksheet->getTitle();
                         $highestRow = $worksheet->getHighestRow();
                         // e.g. 10
                         $highestColumn = $worksheet->getHighestColumn();
                         // e.g 'F'
                         $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
                         $log .= "Feuille : '" . $worksheetTitle . "' trouvée contenant " . $highestRow . ' lignes et ' . $highestColumnIndex . ' colonnes avec comme plus grand index ' . $highestColumn . ' <br>';
                         if (\trim($worksheetTitle) == 'Sage') {
                             $activeSheetIndex = $i;
                         }
                         $i++;
                     }
                     if ($activeSheetIndex == -1) {
                         $log .= "Aucune Feuille de Titre 'Sage' trouvée tentative d'import depuis le première Feuille<br>";
                         $activeSheetIndex = 0;
                     }
                     $excelObj->setActiveSheetIndex($activeSheetIndex);
                     $customersConstStr = $em->getRepository('AcfDataBundle:ConstantStr')->findOneBy(array('name' => 'customersPrefix'));
                     if (null == $customersConstStr) {
                         $customersConstStr = new ConstantStr();
                         $customersConstStr->setName('customersPrefix');
                         $customersConstStr->setValue('411');
                         $em->persist($customersConstStr);
                         $em->flush();
                     }
                     $customersPrefix = $customersConstStr->getValue();
                     $customersPrefixNum = \intval($customersPrefix) * 1000000000;
                     $worksheet = $excelObj->getActiveSheet();
                     $highestRow = $worksheet->getHighestRow();
                     $lineRead = 0;
                     $salesNew = 0;
                     $lineUnprocessed = 0;
                     $lineError = 0;
                     $company = $mbsale->getCompany();
                     $accounts = $em->getRepository('AcfDataBundle:Account')->getAllByCompany($company);
                     $customers = $em->getRepository('AcfDataBundle:Customer')->getAllByCompany($company);
                     $withholdings = $em->getRepository('AcfDataBundle:Withholding')->getAllByCompany($company);
                     $prevbill = null;
                     $haspreverror = false;
                     for ($row = 1; $row <= $highestRow; $row++) {
                         $lineRead++;
                         $dtActivation = \PHPExcel_Shared_Date::ExcelToPHPObject($worksheet->getCellByColumnAndRow(1, $row)->getValue());
                         $bill = \trim(\strval($worksheet->getCellByColumnAndRow(2, $row)->getValue()));
                         $customerNum = \intval($worksheet->getCellByColumnAndRow(4, $row)->getValue());
                         $label = \trim(\strval($worksheet->getCellByColumnAndRow(5, $row)->getValue()));
                         $vat = \floatval($worksheet->getCellByColumnAndRow(7, $row)->getValue());
                         $stamp = \floatval($worksheet->getCellByColumnAndRow(8, $row)->getValue());
                         $balanceTtc = \floatval($worksheet->getCellByColumnAndRow(9, $row)->getValue());
                         $vatInfo = \trim(\strval($worksheet->getCellByColumnAndRow(10, $row)->getValue()));
                         $regime = \trim(\strval($worksheet->getCellByColumnAndRow(11, $row)->getValue()));
                         $withholdingValue = \trim(\strval($worksheet->getCellByColumnAndRow(13, $row)->getValue() * 100));
                         $balanceNet = \floatval($worksheet->getCellByColumnAndRow(14, $row)->getValue());
                         $paymentType = \trim(\strval($worksheet->getCellByColumnAndRow(15, $row)->getValue()));
                         $dtPayment = \PHPExcel_Shared_Date::ExcelToPHPObject($worksheet->getCellByColumnAndRow(16, $row)->getValue());
                         $accountLabel = \trim(\strval($worksheet->getCellByColumnAndRow(17, $row)->getValue()));
                         $status = \trim(\strval($worksheet->getCellByColumnAndRow(18, $row)->getValue()));
                         $otherInfos = \trim(\strval($worksheet->getCellByColumnAndRow(19, $row)->getValue()));
                         if ($customerNum != '' && \is_numeric($customerNum)) {
                             $customerNum = \intval($customerNum) - $customersPrefixNum;
                         }
                         $haserror = false;
                         if (null == $dtActivation) {
                             $haserror = true;
                             $log .= 'ligne ' . $lineRead . ", erreur : Date d'activation<br>";
                         }
                         if ($bill == '') {
                             $haserror = true;
                             $log .= 'ligne ' . $lineRead . ', erreur : Numéro Facture<br>';
                         }
                         if ($customerNum == '' || $customerNum <= 0) {
                             $haserror = true;
                             $oldcustomnum = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
                             $log .= 'ligne ' . $lineRead . ', erreur : Numéro Client (' . $oldcustomnum . ')<br>';
                         } else {
                             $customer = null;
                             $knownCustomer = false;
                             foreach ($customers as $s) {
                                 if ($s->getNumber() == $customerNum) {
                                     $knownCustomer = true;
                                     $customer = $s;
                                 }
                             }
                             if ($knownCustomer == false) {
                                 $haserror = true;
                                 $log .= 'ligne ' . $lineRead . ', erreur : Client Inconnu<br>';
                             }
                         }
                         if ($label == '') {
                             $haserror = true;
                             $log .= 'ligne ' . $lineRead . ', erreur : Libélé<br>';
                         }
                         if ($vat < 0) {
                             $haserror = true;
                             $log .= 'ligne ' . $lineRead . ', erreur : TVA<br>';
                         }
                         if ($stamp < 0) {
                             $haserror = true;
                             $log .= 'ligne ' . $lineRead . ', erreur : Timbre<br>';
                         }
                         if ($balanceTtc < 0) {
                             $haserror = true;
                             $log .= 'ligne ' . $lineRead . ', erreur : TTC<br>';
                         }
                         if ($vatInfo == $this->translate('Transaction.vatInfo.0')) {
                             $vatInfo = 0;
                         } elseif ($vatInfo == $this->translate('Transaction.vatInfo.6')) {
                             $vatInfo = 6;
                         } elseif ($vatInfo == $this->translate('Transaction.vatInfo.12')) {
                             $vatInfo = 12;
                         } elseif ($vatInfo == $this->translate('Transaction.vatInfo.18')) {
                             $vatInfo = 18;
                         } else {
                             $vatInfo = 0;
                             $log .= 'ligne ' . $lineRead . ', erreur (ignorée) : TVA PR INFO inconnu => ' . $this->translate('Transaction.vatInfo.0') . '<br>';
                         }
                         if ($regime == $this->translate('Sale.regime.0')) {
                             $regime = 0;
                         } elseif ($regime == $this->translate('Sale.regime.1')) {
                             $regime = 1;
                         } elseif ($regime == $this->translate('Sale.regime.2')) {
                             $regime = 2;
                         } elseif ($regime == $this->translate('Sale.regime.3')) {
                             $regime = 3;
                         } else {
                             $regime = 0;
                             $log .= 'ligne ' . $lineRead . ', erreur (ignorée) : Régime inconnu => ' . $this->translate('Sale.regime.0') . '<br>';
                         }
                         $withholding = null;
                         $knownWithholding = false;
                         foreach ($withholdings as $w) {
                             if ($w->getValue() == $withholdingValue || $w->getLabel() == $withholdingValue) {
                                 $knownWithholding = true;
                                 $withholding = $w;
                             }
                         }
                         if ($knownWithholding == false) {
                             $haserror = true;
                             $log .= 'ligne ' . $lineRead . ', erreur : Retenue Inconnue ' . $withholdingValue . '<br>';
                         }
                         if ($balanceNet < 0) {
                             $haserror = true;
                             $log .= 'ligne ' . $lineRead . ', erreur : Net à Encaisser<br>';
                         }
                         if ($paymentType == $this->translate('Transaction.paymentType.0')) {
                             $paymentType = 0;
                         } elseif ($paymentType == $this->translate('Transaction.paymentType.1')) {
                             $paymentType = 1;
                         } elseif ($paymentType == $this->translate('Transaction.paymentType.2')) {
                             $paymentType = 2;
                         } elseif ($paymentType == $this->translate('Transaction.paymentType.3')) {
                             $paymentType = 3;
                         } elseif ($paymentType == $this->translate('Transaction.paymentType.4')) {
                             $paymentType = 4;
                         } else {
                             $paymentType = 0;
                             $log .= 'ligne ' . $lineRead . ", erreur (ignorée) : Type d'Encaissement inconnu => " . $this->translate('Transaction.paymentType.0') . '<br>';
                         }
                         if (null == $dtPayment) {
                             $haserror = true;
                             $log .= 'ligne ' . $lineRead . ", erreur : Date d'Encaissement<br>";
                         }
                         $account = null;
                         $knownAccount = false;
                         foreach ($accounts as $a) {
                             if ($a->getLabel() == $accountLabel) {
                                 $knownAccount = true;
                                 $account = $a;
                             }
                         }
                         if ($knownAccount == false) {
                             $haserror = true;
                             $log .= 'ligne ' . $lineRead . ', erreur : Banque/Caisse Inconnue<br>';
                         }
                         if ($status == $this->translate('Transaction.transactionStatus.0')) {
                             $status = 0;
                         } elseif ($status == $this->translate('Transaction.transactionStatus.1')) {
                             $status = 1;
                         } elseif ($status == $this->translate('Transaction.transactionStatus.10')) {
                             $status = 10;
                         } else {
                             $status = 0;
                             $log .= 'ligne ' . $lineRead . ', erreur (ignorée) : Etat inconnu => ' . $this->translate('Transaction.transactionStatus.0') . '<br>';
                         }
                         if ($haserror == false) {
                             if ($bill != $prevbill) {
                                 $sale = $em->getRepository('AcfDataBundle:Sale')->findOneBy(array('monthlyBalance' => $mbsale, 'bill' => $bill));
                                 if (null == $sale) {
                                     $salesNew++;
                                     $sale = new Sale();
                                     $sale->setMonthlyBalance($mbsale);
                                     $sale->setNumber($mbsale->getCount());
                                     $sale->setDtActivation($dtActivation);
                                     $sale->setBill($bill);
                                     $sale->setRelation($customer);
                                     $sale->setLabel($label);
                                     $sale->setVat($vat);
                                     $sale->setVatDevise($vat);
                                     $sale->setStamp($stamp);
                                     $sale->setStampDevise($stamp);
                                     $sale->setBalanceTtc($balanceTtc);
                                     $sale->setBalanceTtcDevise($balanceTtc);
                                     $sale->setVatInfo($vatInfo);
                                     $sale->setRegime($regime);
                                     $sale->setWithholding($withholding);
                                     $sale->setBalanceNet($balanceNet);
                                     $sale->setBalanceNetDevise($balanceNet);
                                     $sale->setPaymentType($paymentType);
                                     $sale->setDtPayment($dtPayment);
                                     $sale->setAccount($account);
                                     $sale->setTransactionStatus($status);
                                     $sale->setOtherInfos($otherInfos);
                                     $em->persist($sale);
                                     $mbsale->updateCount();
                                     $em->persist($mbsale);
                                     $haspreverror = false;
                                 } else {
                                     $lineUnprocessed++;
                                     $log .= "l'Achat à la ligne " . $lineRead . ' existe déjà<br>';
                                     $haspreverror = true;
                                 }
                             } else {
                                 if ($haspreverror == false) {
                                     $secondaryVat = new SecondaryVat();
                                     $secondaryVat->setSale($sale);
                                     $secondaryVat->setBalanceNet($balanceNet);
                                     $secondaryVat->setBalanceTtc($balanceTtc);
                                     $secondaryVat->setVat($vat);
                                     $secondaryVat->setVatInfo($vatInfo);
                                     $em->persist($secondaryVat);
                                 } else {
                                     $lineUnprocessed++;
                                     $log .= "l'Achat à la ligne " . $lineRead . ' existe déjà<br>';
                                     $haspreverror = true;
                                 }
                             }
                         } else {
                             $lineError++;
                             $log .= 'la ligne ' . $lineRead . ' contient des erreurs<br>';
                         }
                         $prevbill = $bill;
                     }
                     $em->flush();
                     $log .= $lineRead . ' lignes lues<br>';
                     $log .= $salesNew . ' nouvelles Ventes<br>';
                     $log .= $lineUnprocessed . ' Ventes déjà dans la base<br>';
                     $log .= $lineError . ' lignes contenant des erreurs<br>';
                     // */
                     $this->flashMsgSession('log', $log);
                     $this->flashMsgSession('success', $this->translate('Sale.import.success'));
                     $this->gvars['tabActive'] = 1;
                     $this->getSession()->set('tabActive', 1);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($mbsale);
                     $this->flashMsgSession('error', $this->translate('Sale.import.failure'));
                 }
             } elseif (isset($reqData['SaleNewForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $this->gvars['stabActive'] = 2;
                 $this->getSession()->set('stabActive', 2);
                 $saleNewForm->handleRequest($request);
                 if ($saleNewForm->isValid()) {
                     $sale->setNumber($mbsale->getCount());
                     if ($sale->getDevise() == 'TND') {
                         $sale->setConversionRate(1);
                         $sale->setVatDevise($sale->getVat());
                         $sale->setStampDevise($sale->getStamp());
                         $sale->setBalanceTtcDevise($sale->getBalanceTtc());
                         $sale->setBalanceNetDevise($sale->getBalanceNet());
                     } else {
                         $sale->setVat($sale->getVatDevise() * $sale->getConversionRate());
                         $sale->setStamp($sale->getStampDevise() * $sale->getConversionRate());
                         $sale->setBalanceTtc($sale->getBalanceTtcDevise() * $sale->getConversionRate());
                         $sale->setBalanceNet($sale->getBalanceNetDevise() * $sale->getConversionRate());
                     }
                     foreach ($saleNewForm->get('docs') as $docNewForm) {
                         $docFile = $docNewForm['fileName']->getData();
                         $docDir = $this->getParameter('kernel.root_dir') . '/../web/res/docs';
                         $originalName = $docFile->getClientOriginalName();
                         $fileName = sha1(uniqid(mt_rand(), true)) . '.' . strtolower($docFile->getClientOriginalExtension());
                         $mimeType = $docFile->getMimeType();
                         $docFile->move($docDir, $fileName);
                         $size = filesize($docDir . '/' . $fileName);
                         $md5 = md5_file($docDir . '/' . $fileName);
                         $doc = $docNewForm->getData();
                         $doc->setCompany($mbsale->getCompany());
                         $doc->setFileName($fileName);
                         $doc->setOriginalName($originalName);
                         $doc->setSize($size);
                         $doc->setMimeType($mimeType);
                         $doc->setMd5($md5);
                         $doc->setDescription($docNewForm['description']->getData());
                         $em->persist($doc);
                         $sale->addDoc($doc);
                     }
                     $em->persist($sale);
                     foreach ($saleNewForm->get('secondaryVats') as $secondaryVatNewForm) {
                         $secondaryVat = $secondaryVatNewForm->getData();
                         $secondaryVat->setSale($sale);
                         $sale->addSecondaryVat($secondaryVat);
                         $em->persist($secondaryVat);
                         $em->persist($sale);
                     }
                     $em->flush();
                     $mbsale->updateCount();
                     $em->persist($mbsale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.add.success', array('%sale%' => $sale->getNumber())));
                     $this->gvars['tabActive'] = 1;
                     $this->getSession()->set('tabActive', 1);
                     $this->gvars['stabActive'] = 1;
                     $this->getSession()->set('stabActive', 1);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($mbsale);
                     $this->flashMsgSession('error', $this->translate('Sale.add.failure'));
                 }
             } elseif (isset($reqData['MBSaleUpdateCountForm'])) {
                 $this->gvars['tabActive'] = 3;
                 $this->getSession()->set('tabActive', 3);
                 $mbsaleUpdateCountForm->handleRequest($request);
                 if ($mbsaleUpdateCountForm->isValid()) {
                     $em->persist($mbsale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('MBSale.edit.success', array('%mbsale%' => $mbsale->getRef())));
                     $this->traceEntity($cloneMBSale, $mbsale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($mbsale);
                     $this->flashMsgSession('error', $this->translate('MBSale.edit.failure', array('%mbsale%' => $mbsale->getRef())));
                 }
             } elseif (isset($reqData['DocNewForm'])) {
                 $this->gvars['tabActive'] = 4;
                 $this->getSession()->set('tabActive', 4);
                 $this->gvars['stabActive'] = 1;
                 $this->getSession()->set('stabActive', 1);
                 $docNewForm->handleRequest($request);
                 if ($docNewForm->isValid()) {
                     $docFiles = $docNewForm['fileName']->getData();
                     $docs = array();
                     $docDir = $this->getParameter('kernel.root_dir') . '/../web/res/docs';
                     $docNames = '';
                     foreach ($docFiles as $docFile) {
                         $originalName = $docFile->getClientOriginalName();
                         $fileName = sha1(uniqid(mt_rand(), true)) . '.' . strtolower($docFile->getClientOriginalExtension());
                         $mimeType = $docFile->getMimeType();
                         $docFile->move($docDir, $fileName);
                         $size = filesize($docDir . '/' . $fileName);
                         $md5 = md5_file($docDir . '/' . $fileName);
                         $doc = new Doc();
                         $doc->setCompany($mbsale->getCompany());
                         $doc->setFileName($fileName);
                         $doc->setOriginalName($originalName);
                         $doc->setSize($size);
                         $doc->setMimeType($mimeType);
                         $doc->setMd5($md5);
                         $doc->setDescription($docNewForm['description']->getData());
                         $em->persist($doc);
                         $mbsale->addDoc($doc);
                         $docNames .= $doc->getOriginalName() . ' ';
                         $docs[] = $doc;
                     }
                     $em->persist($mbsale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Doc.add.success', array('%doc%' => $docNames)));
                     $from = $this->getParameter('mail_from');
                     $fromName = $this->getParameter('mail_from_name');
                     $subject = $this->translate('_mail.newdocsCloud.subject', array(), 'messages');
                     $company = $mbsale->getCompany();
                     $acfCloudRole = $em->getRepository('AcfDataBundle:Role')->findOneBy(array('name' => 'ROLE_CLIENT1'));
                     $users = array();
                     foreach ($company->getUsers() as $user) {
                         if ($user->hasRole($acfCloudRole)) {
                             $users[] = $user;
                         }
                     }
                     if (\count($users) != 0) {
                         foreach ($users as $user) {
                             $mvars = array();
                             $mvars['company'] = $company;
                             $mvars['docs'] = $docs;
                             $message = \Swift_Message::newInstance();
                             $message->setFrom($from, $fromName);
                             $message->addTo($user->getEmail(), $user->getFullname());
                             $message->setSubject($subject);
                             $mvars['logo'] = $message->embed(\Swift_Image::fromPath($this->getParameter('kernel.root_dir') . '/../web/bundles/acfres/images/logo_acf.jpg'));
                             $message->setBody($this->renderView('AcfAdminBundle:Doc:sendmail.html.twig', $mvars), 'text/html');
                             $this->sendmail($message);
                         }
                     }
                     $this->gvars['stabActive'] = 3;
                     $this->getSession()->set('stabActive', 3);
                     $this->traceEntity($cloneMBSale, $mbsale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($mbsale);
                     $this->flashMsgSession('error', $this->translate('Doc.add.failure'));
                 }
             } elseif (isset($reqData['MBSaleUpdateDocsForm'])) {
                 $this->gvars['tabActive'] = 4;
                 $this->getSession()->set('tabActive', 4);
                 $this->gvars['stabActive'] = 2;
                 $this->getSession()->set('stabActive', 2);
                 $mbsaleUpdateDocsForm->handleRequest($request);
                 if ($mbsaleUpdateDocsForm->isValid()) {
                     $em->persist($mbsale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('MBSale.edit.success', array('%mbsale%' => $mbsale->getRef())));
                     $this->gvars['stabActive'] = 3;
                     $this->getSession()->set('stabActive', 3);
                     $this->traceEntity($cloneMBSale, $mbsale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($mbsale);
                     $this->flashMsgSession('error', $this->translate('MBSale.edit.failure', array('%mbsale%' => $mbsale->getRef())));
                 }
             }
             $this->gvars['mbsale'] = $mbsale;
             $this->gvars['sale'] = $sale;
             $this->gvars['doc'] = $doc;
             $this->gvars['SaleNewForm'] = $saleNewForm->createView();
             $this->gvars['SaleImportForm'] = $saleImportForm->createView();
             $this->gvars['MBSaleUpdateCountForm'] = $mbsaleUpdateCountForm->createView();
             $this->gvars['MBSaleUpdateDocsForm'] = $mbsaleUpdateDocsForm->createView();
             $this->gvars['DocNewForm'] = $docNewForm->createView();
             $customersConstStr = $em->getRepository('AcfDataBundle:ConstantStr')->findOneBy(array('name' => 'customersPrefix'));
             if (null == $customersConstStr) {
                 $customersConstStr = new ConstantStr();
                 $customersConstStr->setName('customersPrefix');
                 $customersConstStr->setValue('411');
                 $em->persist($customersConstStr);
                 $em->flush();
             }
             $customersPrefix = $customersConstStr->getValue();
             $this->gvars['customersPrefix'] = $customersPrefix;
             $this->gvars['pagetitle'] = $this->translate('pagetitle.mbsale.edit', array('%mbsale%' => $mbsale->getRef()));
             $this->gvars['pagetitle_txt'] = $this->translate('pagetitle.mbsale.edit.txt', array('%mbsale%' => $mbsale->getRef()));
             return $this->renderResponse('AcfAdminBundle:MBSale:edit.html.twig', $this->gvars);
         }
     } catch (\Exception $e) {
         $logger = $this->getLogger();
         $logger->addCritical($e->getLine() . ' ' . $e->getMessage() . ' ' . $e->getTraceAsString());
     }
     return $this->redirect($urlFrom);
 }
Esempio n. 5
0
 /**
  *
  * @param string $uid
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  */
 public function editPostAction($uid)
 {
     $urlFrom = $this->getReferer();
     if (null == $urlFrom || trim($urlFrom) == '') {
         $urlFrom = $this->generateUrl('_admin_company_list');
     }
     $em = $this->getEntityManager();
     try {
         $sale = $em->getRepository('AcfDataBundle:Sale')->find($uid);
         if (null == $sale) {
             $this->flashMsgSession('warning', $this->translate('Sale.edit.notfound'));
         } else {
             $traces = $em->getRepository('AcfDataBundle:Trace')->getAllByEntityId($sale->getId(), Trace::AE_SALE);
             $this->gvars['traces'] = array_reverse($traces);
             $saleUpdateNumberForm = $this->createForm(SaleUpdateNumberTForm::class, $sale);
             $saleUpdateDtActivationForm = $this->createForm(SaleUpdateDtActivationTForm::class, $sale);
             $saleUpdateBillForm = $this->createForm(SaleUpdateBillTForm::class, $sale);
             $saleUpdateRelationForm = $this->createForm(SaleUpdateRelationTForm::class, $sale, array('monthlybalance' => $sale->getMonthlyBalance()));
             $saleUpdateLabelForm = $this->createForm(SaleUpdateLabelTForm::class, $sale);
             $saleUpdateDeviseForm = $this->createForm(SaleUpdateDeviseTForm::class, $sale);
             $saleUpdateConversionRateForm = $this->createForm(SaleUpdateConversionRateTForm::class, $sale);
             $saleUpdateVatForm = $this->createForm(SaleUpdateVatTForm::class, $sale);
             $saleUpdateVatDeviseForm = $this->createForm(SaleUpdateVatDeviseTForm::class, $sale);
             $saleUpdateStampForm = $this->createForm(SaleUpdateStampTForm::class, $sale);
             $saleUpdateStampDeviseForm = $this->createForm(SaleUpdateStampDeviseTForm::class, $sale);
             $saleUpdateBalanceTtcForm = $this->createForm(SaleUpdateBalanceTtcTForm::class, $sale);
             $saleUpdateBalanceTtcDeviseForm = $this->createForm(SaleUpdateBalanceTtcDeviseTForm::class, $sale);
             $saleUpdateVatInfoForm = $this->createForm(SaleUpdateVatInfoTForm::class, $sale);
             $saleUpdateRegimeForm = $this->createForm(SaleUpdateRegimeTForm::class, $sale);
             $saleUpdateWithholdingForm = $this->createForm(SaleUpdateWithholdingTForm::class, $sale, array('monthlybalance' => $sale->getMonthlyBalance()));
             $saleUpdateBalanceNetForm = $this->createForm(SaleUpdateBalanceNetTForm::class, $sale);
             $saleUpdateBalanceNetDeviseForm = $this->createForm(SaleUpdateBalanceNetDeviseTForm::class, $sale);
             $saleUpdatePaymentTypeForm = $this->createForm(SaleUpdatePaymentTypeTForm::class, $sale);
             $saleUpdateTransactionStatusForm = $this->createForm(SaleUpdateTransactionStatusTForm::class, $sale);
             $saleUpdateValidatedForm = $this->createForm(SaleUpdateValidatedTForm::class, $sale);
             $saleUpdateAccountForm = $this->createForm(SaleUpdateAccountTForm::class, $sale, array('monthlybalance' => $sale->getMonthlyBalance()));
             $saleUpdateOtherInfosForm = $this->createForm(SaleUpdateOtherInfosTForm::class, $sale);
             $saleUpdateDocsForm = $this->createForm(SaleUpdateDocsTForm::class, $sale, array('company' => $sale->getCompany()));
             $secondaryVat = new SecondaryVat();
             $secondaryVat->setSale($sale);
             $secondaryVatNewForm = $this->createForm(SecondaryVatNewTForm::class, $secondaryVat, array('sale' => $sale));
             $doc = new Doc();
             $doc->setCompany($sale->getCompany());
             $docNewForm = $this->createForm(DocNewTForm::class, $doc, array('company' => $sale->getCompany()));
             $this->gvars['tabActive'] = $this->getSession()->get('tabActive', 2);
             $this->getSession()->remove('tabActive');
             $this->gvars['stabActive'] = $this->getSession()->get('stabActive', 1);
             $this->getSession()->remove('stabActive');
             $request = $this->getRequest();
             $reqData = $request->request->all();
             $cloneSale = clone $sale;
             if (isset($reqData['SaleUpdateNumberForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateNumberForm->handleRequest($request);
                 if ($saleUpdateNumberForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateDtActivationForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateDtActivationForm->handleRequest($request);
                 if ($saleUpdateDtActivationForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateBillForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateBillForm->handleRequest($request);
                 if ($saleUpdateBillForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateRelationForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateRelationForm->handleRequest($request);
                 if ($saleUpdateRelationForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateLabelForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateLabelForm->handleRequest($request);
                 if ($saleUpdateLabelForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateDeviseForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateDeviseForm->handleRequest($request);
                 if ($saleUpdateDeviseForm->isValid()) {
                     if ($sale->getDevise() == 'TND') {
                         $sale->setConversionRate(1);
                         $sale->setVatDevise($sale->getVat());
                         $sale->setStampDevise($sale->getStamp());
                         $sale->setBalanceTtcDevise($sale->getBalanceTtc());
                         $sale->setBalanceNetDevise($sale->getBalanceNet());
                     } else {
                         $sale->setVat($sale->getVatDevise() * $sale->getConversionRate());
                         $sale->setStamp($sale->getStampDevise() * $sale->getConversionRate());
                         $sale->setBalanceTtc($sale->getBalanceTtcDevise() * $sale->getConversionRate());
                         $sale->setBalanceNet($sale->getBalanceNetDevise() * $sale->getConversionRate());
                     }
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateConversionRateForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateConversionRateForm->handleRequest($request);
                 if ($saleUpdateConversionRateForm->isValid()) {
                     if ($sale->getDevise() == 'TND') {
                         $sale->setConversionRate(1);
                         $sale->setVatDevise($sale->getVat());
                         $sale->setStampDevise($sale->getStamp());
                         $sale->setBalanceTtcDevise($sale->getBalanceTtc());
                         $sale->setBalanceNetDevise($sale->getBalanceNet());
                     } else {
                         $sale->setVat($sale->getVatDevise() * $sale->getConversionRate());
                         $sale->setStamp($sale->getStampDevise() * $sale->getConversionRate());
                         $sale->setBalanceTtc($sale->getBalanceTtcDevise() * $sale->getConversionRate());
                         $sale->setBalanceNet($sale->getBalanceNetDevise() * $sale->getConversionRate());
                     }
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateVatForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateVatForm->handleRequest($request);
                 if ($saleUpdateVatForm->isValid()) {
                     $sale->setConversionRate(1);
                     $sale->setVatDevise($sale->getVat());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateVatDeviseForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateVatDeviseForm->handleRequest($request);
                 if ($saleUpdateVatDeviseForm->isValid()) {
                     $sale->setVat($sale->getVatDevise() * $sale->getConversionRate());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateStampForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateStampForm->handleRequest($request);
                 if ($saleUpdateStampForm->isValid()) {
                     $sale->setConversionRate(1);
                     $sale->setStampDevise($sale->getStamp());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateStampDeviseForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateStampDeviseForm->handleRequest($request);
                 if ($saleUpdateStampDeviseForm->isValid()) {
                     $sale->setStamp($sale->getStampDevise() * $sale->getConversionRate());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateBalanceTtcForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateBalanceTtcForm->handleRequest($request);
                 if ($saleUpdateBalanceTtcForm->isValid()) {
                     $sale->setConversionRate(1);
                     $sale->setBalanceTtcDevise($sale->getBalanceTtc());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateVatInfoForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateVatInfoForm->handleRequest($request);
                 if ($saleUpdateVatInfoForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateRegimeForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateRegimeForm->handleRequest($request);
                 if ($saleUpdateRegimeForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateWithholdingForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateWithholdingForm->handleRequest($request);
                 if ($saleUpdateWithholdingForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateBalanceNetDeviseForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateBalanceNetDeviseForm->handleRequest($request);
                 if ($saleUpdateBalanceNetDeviseForm->isValid()) {
                     $sale->setBalanceNet($sale->getBalanceNetDevise() * $sale->getConversionRate());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateBalanceNetForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateBalanceNetForm->handleRequest($request);
                 if ($saleUpdateBalanceNetForm->isValid()) {
                     $sale->setConversionRate(1);
                     $sale->setBalanceNetDevise($sale->getBalanceNet());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdatePaymentTypeForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdatePaymentTypeForm->handleRequest($request);
                 if ($saleUpdatePaymentTypeForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateTransactionStatusForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateTransactionStatusForm->handleRequest($request);
                 if ($saleUpdateTransactionStatusForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateValidatedForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateValidatedForm->handleRequest($request);
                 if ($saleUpdateValidatedForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateAccountForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateAccountForm->handleRequest($request);
                 if ($saleUpdateAccountForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateOtherInfosForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateOtherInfosForm->handleRequest($request);
                 if ($saleUpdateOtherInfosForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SecondaryVatNewForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $secondaryVatNewForm->handleRequest($request);
                 if ($secondaryVatNewForm->isValid()) {
                     $em->persist($secondaryVat);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('SecondaryVat.add.success', array('%sale%' => $sale->getNumber(), '%secondaryVat%' => $this->translate('SecondaryVat.vatInfo.' . $secondaryVat->getVatInfo()))));
                     $this->gvars['tabActive'] = 1;
                     $this->getSession()->set('tabActive', 1);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('SecondaryVat.add.failure'));
                 }
             } elseif (isset($reqData['DocNewForm'])) {
                 $this->gvars['tabActive'] = 3;
                 $this->getSession()->set('tabActive', 3);
                 $this->gvars['stabActive'] = 1;
                 $this->getSession()->set('stabActive', 1);
                 $docNewForm->handleRequest($request);
                 if ($docNewForm->isValid()) {
                     $docFiles = $docNewForm['fileName']->getData();
                     $docs = array();
                     $docDir = $this->getParameter('kernel.root_dir') . '/../web/res/docs';
                     $docNames = '';
                     foreach ($docFiles as $docFile) {
                         $originalName = $docFile->getClientOriginalName();
                         $fileName = sha1(uniqid(mt_rand(), true)) . '.' . strtolower($docFile->getClientOriginalExtension());
                         $mimeType = $docFile->getMimeType();
                         $docFile->move($docDir, $fileName);
                         $size = filesize($docDir . '/' . $fileName);
                         $md5 = md5_file($docDir . '/' . $fileName);
                         $doc = new Doc();
                         $doc->setCompany($sale->getCompany());
                         $doc->setFileName($fileName);
                         $doc->setOriginalName($originalName);
                         $doc->setSize($size);
                         $doc->setMimeType($mimeType);
                         $doc->setMd5($md5);
                         $doc->setDescription($docNewForm['description']->getData());
                         $em->persist($doc);
                         $sale->addDoc($doc);
                         $docNames .= $doc->getOriginalName() . ' ';
                         $docs[] = $doc;
                     }
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Doc.add.success', array('%doc%' => $docNames)));
                     $from = $this->getParameter('mail_from');
                     $fromName = $this->getParameter('mail_from_name');
                     $subject = $this->translate('_mail.newdocsCloud.subject', array(), 'messages');
                     $company = $sale->getCompany();
                     $acfCloudRole = $em->getRepository('AcfDataBundle:Role')->findOneBy(array('name' => 'ROLE_CLIENT1'));
                     $users = array();
                     foreach ($company->getUsers() as $user) {
                         if ($user->hasRole($acfCloudRole)) {
                             $users[] = $user;
                         }
                     }
                     if (\count($users) != 0) {
                         foreach ($users as $user) {
                             $mvars = array();
                             $mvars['company'] = $company;
                             $mvars['docs'] = $docs;
                             $message = \Swift_Message::newInstance();
                             $message->setFrom($from, $fromName);
                             $message->addTo($user->getEmail(), $user->getFullname());
                             $message->setSubject($subject);
                             $mvars['logo'] = $message->embed(\Swift_Image::fromPath($this->getParameter('kernel.root_dir') . '/../web/bundles/acfres/images/logo_acf.jpg'));
                             $message->setBody($this->renderView('AcfAdminBundle:Doc:sendmail.html.twig', $mvars), 'text/html');
                             $this->sendmail($message);
                         }
                     }
                     $this->gvars['stabActive'] = 3;
                     $this->getSession()->set('stabActive', 3);
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Doc.add.failure'));
                 }
             } elseif (isset($reqData['SaleUpdateDocsForm'])) {
                 $this->gvars['tabActive'] = 3;
                 $this->getSession()->set('tabActive', 3);
                 $this->gvars['stabActive'] = 2;
                 $this->getSession()->set('stabActive', 2);
                 $saleUpdateDocsForm->handleRequest($request);
                 if ($saleUpdateDocsForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->gvars['stabActive'] = 3;
                     $this->getSession()->set('stabActive', 3);
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             }
             $this->gvars['sale'] = $sale;
             $this->gvars['secondaryVat'] = $secondaryVat;
             $this->gvars['doc'] = $doc;
             $this->gvars['SaleUpdateNumberForm'] = $saleUpdateNumberForm->createView();
             $this->gvars['SaleUpdateDtActivationForm'] = $saleUpdateDtActivationForm->createView();
             $this->gvars['SaleUpdateBillForm'] = $saleUpdateBillForm->createView();
             $this->gvars['SaleUpdateRelationForm'] = $saleUpdateRelationForm->createView();
             $this->gvars['SaleUpdateLabelForm'] = $saleUpdateLabelForm->createView();
             $this->gvars['SaleUpdateDeviseForm'] = $saleUpdateDeviseForm->createView();
             $this->gvars['SaleUpdateConversionRateForm'] = $saleUpdateConversionRateForm->createView();
             $this->gvars['SaleUpdateVatForm'] = $saleUpdateVatForm->createView();
             $this->gvars['SaleUpdateVatDeviseForm'] = $saleUpdateVatDeviseForm->createView();
             $this->gvars['SaleUpdateStampForm'] = $saleUpdateStampForm->createView();
             $this->gvars['SaleUpdateStampDeviseForm'] = $saleUpdateStampDeviseForm->createView();
             $this->gvars['SaleUpdateBalanceTtcForm'] = $saleUpdateBalanceTtcForm->createView();
             $this->gvars['SaleUpdateBalanceTtcDeviseForm'] = $saleUpdateBalanceTtcDeviseForm->createView();
             $this->gvars['SaleUpdateVatInfoForm'] = $saleUpdateVatInfoForm->createView();
             $this->gvars['SaleUpdateRegimeForm'] = $saleUpdateRegimeForm->createView();
             $this->gvars['SaleUpdateWithholdingForm'] = $saleUpdateWithholdingForm->createView();
             $this->gvars['SaleUpdateBalanceNetForm'] = $saleUpdateBalanceNetForm->createView();
             $this->gvars['SaleUpdateBalanceNetDeviseForm'] = $saleUpdateBalanceNetDeviseForm->createView();
             $this->gvars['SaleUpdatePaymentTypeForm'] = $saleUpdatePaymentTypeForm->createView();
             $this->gvars['SaleUpdateTransactionStatusForm'] = $saleUpdateTransactionStatusForm->createView();
             $this->gvars['SaleUpdateValidatedForm'] = $saleUpdateValidatedForm->createView();
             $this->gvars['SaleUpdateAccountForm'] = $saleUpdateAccountForm->createView();
             $this->gvars['SaleUpdateOtherInfosForm'] = $saleUpdateOtherInfosForm->createView();
             $this->gvars['SaleUpdateDocsForm'] = $saleUpdateDocsForm->createView();
             $this->gvars['SecondaryVatNewForm'] = $secondaryVatNewForm->createView();
             $this->gvars['DocNewForm'] = $docNewForm->createView();
             $customersConstStr = $em->getRepository('AcfDataBundle:ConstantStr')->findOneBy(array('name' => 'customersPrefix'));
             if (null == $customersConstStr) {
                 $customersConstStr = new ConstantStr();
                 $customersConstStr->setName('customersPrefix');
                 $customersConstStr->setValue('411');
                 $em->persist($customersConstStr);
                 $em->flush();
             }
             $customersPrefix = $customersConstStr->getValue();
             $this->gvars['customersPrefix'] = $customersPrefix;
             $this->gvars['pagetitle'] = $this->translate('pagetitle.sale.edit', array('%sale%' => $sale->getNumber()));
             $this->gvars['pagetitle_txt'] = $this->translate('pagetitle.sale.edit.txt', array('%sale%' => $sale->getNumber()));
             return $this->renderResponse('AcfAdminBundle:Sale:edit.html.twig', $this->gvars);
         }
     } catch (\Exception $e) {
         $logger = $this->getLogger();
         $logger->addCritical($e->getLine() . ' ' . $e->getMessage() . ' ' . $e->getTraceAsString());
     }
     return $this->redirect($urlFrom);
 }
Esempio n. 6
0
 /**
  *
  * @param string $uid
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  */
 public function editPostAction($uid)
 {
     $urlFrom = $this->getReferer();
     if (null == $urlFrom || trim($urlFrom) == '') {
         $urlFrom = $this->generateUrl('_client_homepage');
     }
     $em = $this->getEntityManager();
     try {
         $sale = $em->getRepository('AcfDataBundle:Sale')->find($uid);
         if (null == $sale) {
             $this->flashMsgSession('warning', $this->translate('Sale.edit.notfound'));
         } else {
             $sc = $this->getSecurityTokenStorage();
             $user = $sc->getToken()->getUser();
             $company = $sale->getMonthlyBalance()->getCompany();
             $companyUser = $em->getRepository('AcfDataBundle:CompanyUser')->findOneBy(array('company' => $company, 'user' => $user));
             if (null == $companyUser || $companyUser->getEditSales() == CompanyUser::CANT) {
                 $this->flashMsgSession('error', $this->translate('CompanyUser.accessForbidden'));
                 return $this->redirect($this->generateUrl('_client_homepage'));
             }
             $this->gvars['companyUser'] = $companyUser;
             $this->gvars['menu_active'] = 'client' . $company->getId();
             $saleUpdateNumberForm = $this->createForm(SaleUpdateNumberTForm::class, $sale);
             $saleUpdateDtActivationForm = $this->createForm(SaleUpdateDtActivationTForm::class, $sale);
             $saleUpdateBillForm = $this->createForm(SaleUpdateBillTForm::class, $sale);
             $saleUpdateRelationForm = $this->createForm(SaleUpdateRelationTForm::class, $sale, array('monthlybalance' => $sale->getMonthlyBalance()));
             $saleUpdateLabelForm = $this->createForm(SaleUpdateLabelTForm::class, $sale);
             $saleUpdateDeviseForm = $this->createForm(SaleUpdateDeviseTForm::class, $sale);
             $saleUpdateConversionRateForm = $this->createForm(SaleUpdateConversionRateTForm::class, $sale);
             $saleUpdateVatForm = $this->createForm(SaleUpdateVatTForm::class, $sale);
             $saleUpdateVatDeviseForm = $this->createForm(SaleUpdateVatDeviseTForm::class, $sale);
             $saleUpdateStampForm = $this->createForm(SaleUpdateStampTForm::class, $sale);
             $saleUpdateStampDeviseForm = $this->createForm(SaleUpdateStampDeviseTForm::class, $sale);
             $saleUpdateBalanceTtcForm = $this->createForm(SaleUpdateBalanceTtcTForm::class, $sale);
             $saleUpdateBalanceTtcDeviseForm = $this->createForm(SaleUpdateBalanceTtcDeviseTForm::class, $sale);
             $saleUpdateVatInfoForm = $this->createForm(SaleUpdateVatInfoTForm::class, $sale);
             $saleUpdateRegimeForm = $this->createForm(SaleUpdateRegimeTForm::class, $sale);
             $saleUpdateWithholdingForm = $this->createForm(SaleUpdateWithholdingTForm::class, $sale, array('monthlybalance' => $sale->getMonthlyBalance()));
             $saleUpdateBalanceNetForm = $this->createForm(SaleUpdateBalanceNetTForm::class, $sale);
             $saleUpdateBalanceNetDeviseForm = $this->createForm(SaleUpdateBalanceNetDeviseTForm::class, $sale);
             $saleUpdatePaymentTypeForm = $this->createForm(SaleUpdatePaymentTypeTForm::class, $sale);
             $saleUpdateTransactionStatusForm = $this->createForm(SaleUpdateTransactionStatusTForm::class, $sale);
             $saleUpdateAccountForm = $this->createForm(SaleUpdateAccountTForm::class, $sale, array('monthlybalance' => $sale->getMonthlyBalance()));
             $saleUpdateOtherInfosForm = $this->createForm(SaleUpdateOtherInfosTForm::class, $sale);
             $saleUpdateDocsForm = $this->createForm(SaleUpdateDocsTForm::class, $sale, array('company' => $sale->getCompany()));
             $secondaryVat = new SecondaryVat();
             $secondaryVat->setSale($sale);
             $secondaryVatNewForm = $this->createForm(SecondaryVatNewTForm::class, $secondaryVat, array('sale' => $sale));
             $doc = new Doc();
             $doc->setCompany($sale->getCompany());
             $docNewForm = $this->createForm(DocNewTForm::class, $doc, array('company' => $sale->getCompany()));
             $this->gvars['tabActive'] = $this->getSession()->get('tabActive', 2);
             $this->getSession()->remove('tabActive');
             $this->gvars['stabActive'] = $this->getSession()->get('stabActive', 1);
             $this->getSession()->remove('stabActive');
             $request = $this->getRequest();
             $reqData = $request->request->all();
             $cloneSale = clone $sale;
             if (isset($reqData['SaleUpdateNumberForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateNumberForm->handleRequest($request);
                 if ($saleUpdateNumberForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateDtActivationForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateDtActivationForm->handleRequest($request);
                 if ($saleUpdateDtActivationForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateBillForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateBillForm->handleRequest($request);
                 if ($saleUpdateBillForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateRelationForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateRelationForm->handleRequest($request);
                 if ($saleUpdateRelationForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateLabelForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateLabelForm->handleRequest($request);
                 if ($saleUpdateLabelForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateDeviseForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateDeviseForm->handleRequest($request);
                 if ($saleUpdateDeviseForm->isValid()) {
                     if ($sale->getDevise() == 'TND') {
                         $sale->setConversionRate(1);
                         $sale->setVatDevise($sale->getVat());
                         $sale->setStampDevise($sale->getStamp());
                         $sale->setBalanceTtcDevise($sale->getBalanceTtc());
                         $sale->setBalanceNetDevise($sale->getBalanceNet());
                     } else {
                         $sale->setVat($sale->getVatDevise() * $sale->getConversionRate());
                         $sale->setStamp($sale->getStampDevise() * $sale->getConversionRate());
                         $sale->setBalanceTtc($sale->getBalanceTtcDevise() * $sale->getConversionRate());
                         $sale->setBalanceNet($sale->getBalanceNetDevise() * $sale->getConversionRate());
                     }
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateConversionRateForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateConversionRateForm->handleRequest($request);
                 if ($saleUpdateConversionRateForm->isValid()) {
                     if ($sale->getDevise() == 'TND') {
                         $sale->setConversionRate(1);
                         $sale->setVatDevise($sale->getVat());
                         $sale->setStampDevise($sale->getStamp());
                         $sale->setBalanceTtcDevise($sale->getBalanceTtc());
                         $sale->setBalanceNetDevise($sale->getBalanceNet());
                     } else {
                         $sale->setVat($sale->getVatDevise() * $sale->getConversionRate());
                         $sale->setStamp($sale->getStampDevise() * $sale->getConversionRate());
                         $sale->setBalanceTtc($sale->getBalanceTtcDevise() * $sale->getConversionRate());
                         $sale->setBalanceNet($sale->getBalanceNetDevise() * $sale->getConversionRate());
                     }
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateVatForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateVatForm->handleRequest($request);
                 if ($saleUpdateVatForm->isValid()) {
                     $sale->setConversionRate(1);
                     $sale->setVatDevise($sale->getVat());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateVatDeviseForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateVatDeviseForm->handleRequest($request);
                 if ($saleUpdateVatDeviseForm->isValid()) {
                     $sale->setVat($sale->getVatDevise() * $sale->getConversionRate());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateStampForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateStampForm->handleRequest($request);
                 if ($saleUpdateStampForm->isValid()) {
                     $sale->setConversionRate(1);
                     $sale->setStampDevise($sale->getStamp());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateStampDeviseForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateStampDeviseForm->handleRequest($request);
                 if ($saleUpdateStampDeviseForm->isValid()) {
                     $sale->setStamp($sale->getStampDevise() * $sale->getConversionRate());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateBalanceTtcForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateBalanceTtcForm->handleRequest($request);
                 if ($saleUpdateBalanceTtcForm->isValid()) {
                     $sale->setConversionRate(1);
                     $sale->setBalanceTtcDevise($sale->getBalanceTtc());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateVatInfoForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateVatInfoForm->handleRequest($request);
                 if ($saleUpdateVatInfoForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateRegimeForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateRegimeForm->handleRequest($request);
                 if ($saleUpdateRegimeForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateWithholdingForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateWithholdingForm->handleRequest($request);
                 if ($saleUpdateWithholdingForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateBalanceNetDeviseForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateBalanceNetDeviseForm->handleRequest($request);
                 if ($saleUpdateBalanceNetDeviseForm->isValid()) {
                     $sale->setBalanceNet($sale->getBalanceNetDevise() * $sale->getConversionRate());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateBalanceNetForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateBalanceNetForm->handleRequest($request);
                 if ($saleUpdateBalanceNetForm->isValid()) {
                     $sale->setConversionRate(1);
                     $sale->setBalanceNetDevise($sale->getBalanceNet());
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdatePaymentTypeForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdatePaymentTypeForm->handleRequest($request);
                 if ($saleUpdatePaymentTypeForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateTransactionStatusForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateTransactionStatusForm->handleRequest($request);
                 if ($saleUpdateTransactionStatusForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateAccountForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateAccountForm->handleRequest($request);
                 if ($saleUpdateAccountForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SaleUpdateOtherInfosForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $saleUpdateOtherInfosForm->handleRequest($request);
                 if ($saleUpdateOtherInfosForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             } elseif (isset($reqData['SecondaryVatNewForm'])) {
                 $this->gvars['tabActive'] = 2;
                 $this->getSession()->set('tabActive', 2);
                 $secondaryVatNewForm->handleRequest($request);
                 if ($secondaryVatNewForm->isValid()) {
                     $em->persist($secondaryVat);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('SecondaryVat.add.success', array('%sale%' => $sale->getNumber(), '%secondaryVat%' => $this->translate('SecondaryVat.vatInfo.' . $secondaryVat->getVatInfo()))));
                     $this->gvars['tabActive'] = 1;
                     $this->getSession()->set('tabActive', 1);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('SecondaryVat.add.failure'));
                 }
             } elseif (isset($reqData['DocNewForm'])) {
                 $this->gvars['tabActive'] = 3;
                 $this->getSession()->set('tabActive', 3);
                 $this->gvars['stabActive'] = 1;
                 $this->getSession()->set('stabActive', 1);
                 $docNewForm->handleRequest($request);
                 if ($docNewForm->isValid()) {
                     $docFiles = $docNewForm['fileName']->getData();
                     $docs = array();
                     $docDir = $this->getParameter('kernel.root_dir') . '/../web/res/docs';
                     $docNames = '';
                     foreach ($docFiles as $docFile) {
                         $originalName = $docFile->getClientOriginalName();
                         $fileName = sha1(uniqid(mt_rand(), true)) . '.' . strtolower($docFile->getClientOriginalExtension());
                         $mimeType = $docFile->getMimeType();
                         $docFile->move($docDir, $fileName);
                         $size = filesize($docDir . '/' . $fileName);
                         $md5 = md5_file($docDir . '/' . $fileName);
                         $doc = new Doc();
                         $doc->setCompany($sale->getCompany());
                         $doc->setFileName($fileName);
                         $doc->setOriginalName($originalName);
                         $doc->setSize($size);
                         $doc->setMimeType($mimeType);
                         $doc->setMd5($md5);
                         $doc->setDescription($docNewForm['description']->getData());
                         $em->persist($doc);
                         $sale->addDoc($doc);
                         $docs[] = $doc;
                         $docNames .= $doc->getOriginalName() . ' ';
                     }
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Doc.add.success', array('%doc%' => $docNames)));
                     $this->newDocNotifyAdmin($sale, $docs);
                     $this->gvars['stabActive'] = 3;
                     $this->getSession()->set('stabActive', 3);
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Doc.add.failure'));
                 }
             } elseif (isset($reqData['SaleUpdateDocsForm'])) {
                 $this->gvars['tabActive'] = 3;
                 $this->getSession()->set('tabActive', 3);
                 $this->gvars['stabActive'] = 2;
                 $this->getSession()->set('stabActive', 2);
                 $saleUpdateDocsForm->handleRequest($request);
                 if ($saleUpdateDocsForm->isValid()) {
                     $em->persist($sale);
                     $em->flush();
                     $this->flashMsgSession('success', $this->translate('Sale.edit.success', array('%sale%' => $sale->getNumber())));
                     $this->gvars['stabActive'] = 3;
                     $this->getSession()->set('stabActive', 3);
                     $this->traceEntity($cloneSale, $sale);
                     return $this->redirect($urlFrom);
                 } else {
                     $em->refresh($sale);
                     $this->flashMsgSession('error', $this->translate('Sale.edit.failure', array('%sale%' => $sale->getNumber())));
                 }
             }
             $this->gvars['sale'] = $sale;
             $this->gvars['secondaryVat'] = $secondaryVat;
             $this->gvars['doc'] = $doc;
             $this->gvars['SaleUpdateNumberForm'] = $saleUpdateNumberForm->createView();
             $this->gvars['SaleUpdateDtActivationForm'] = $saleUpdateDtActivationForm->createView();
             $this->gvars['SaleUpdateBillForm'] = $saleUpdateBillForm->createView();
             $this->gvars['SaleUpdateRelationForm'] = $saleUpdateRelationForm->createView();
             $this->gvars['SaleUpdateLabelForm'] = $saleUpdateLabelForm->createView();
             $this->gvars['SaleUpdateDeviseForm'] = $saleUpdateDeviseForm->createView();
             $this->gvars['SaleUpdateConversionRateForm'] = $saleUpdateConversionRateForm->createView();
             $this->gvars['SaleUpdateVatForm'] = $saleUpdateVatForm->createView();
             $this->gvars['SaleUpdateVatDeviseForm'] = $saleUpdateVatDeviseForm->createView();
             $this->gvars['SaleUpdateStampForm'] = $saleUpdateStampForm->createView();
             $this->gvars['SaleUpdateStampDeviseForm'] = $saleUpdateStampDeviseForm->createView();
             $this->gvars['SaleUpdateBalanceTtcForm'] = $saleUpdateBalanceTtcForm->createView();
             $this->gvars['SaleUpdateBalanceTtcDeviseForm'] = $saleUpdateBalanceTtcDeviseForm->createView();
             $this->gvars['SaleUpdateVatInfoForm'] = $saleUpdateVatInfoForm->createView();
             $this->gvars['SaleUpdateRegimeForm'] = $saleUpdateRegimeForm->createView();
             $this->gvars['SaleUpdateWithholdingForm'] = $saleUpdateWithholdingForm->createView();
             $this->gvars['SaleUpdateBalanceNetForm'] = $saleUpdateBalanceNetForm->createView();
             $this->gvars['SaleUpdateBalanceNetDeviseForm'] = $saleUpdateBalanceNetDeviseForm->createView();
             $this->gvars['SaleUpdatePaymentTypeForm'] = $saleUpdatePaymentTypeForm->createView();
             $this->gvars['SaleUpdateTransactionStatusForm'] = $saleUpdateTransactionStatusForm->createView();
             $this->gvars['SaleUpdateAccountForm'] = $saleUpdateAccountForm->createView();
             $this->gvars['SaleUpdateOtherInfosForm'] = $saleUpdateOtherInfosForm->createView();
             $this->gvars['SaleUpdateDocsForm'] = $saleUpdateDocsForm->createView();
             $this->gvars['SecondaryVatNewForm'] = $secondaryVatNewForm->createView();
             $this->gvars['DocNewForm'] = $docNewForm->createView();
             $customersConstStr = $em->getRepository('AcfDataBundle:ConstantStr')->findOneBy(array('name' => 'customersPrefix'));
             if (null == $customersConstStr) {
                 $customersConstStr = new ConstantStr();
                 $customersConstStr->setName('customersPrefix');
                 $customersConstStr->setValue('411');
                 $em->persist($customersConstStr);
                 $em->flush();
             }
             $customersPrefix = $customersConstStr->getValue();
             $this->gvars['customersPrefix'] = $customersPrefix;
             $this->gvars['pagetitle'] = $this->translate('pagetitle.sale.edit', array('%sale%' => $sale->getNumber()));
             $this->gvars['pagetitle_txt'] = $this->translate('pagetitle.sale.edit.txt', array('%sale%' => $sale->getNumber()));
             return $this->renderResponse('AcfClientBundle:Sale:edit.html.twig', $this->gvars);
         }
     } catch (\Exception $e) {
         $logger = $this->getLogger();
         $logger->addCritical($e->getLine() . ' ' . $e->getMessage() . ' ' . $e->getTraceAsString());
     }
     return $this->redirect($urlFrom);
 }