您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

83 行
3.0KB

  1. <?php
  2. namespace domain\Document\Document;
  3. use domain\Producer\Producer\ProducerSolver;
  4. use domain\User\User\User;
  5. use domain\User\User\UserSolver;
  6. use domain\_\AbstractGenerator;
  7. class DocumentReferenceGenerator extends AbstractGenerator
  8. {
  9. protected DocumentSolver $documentSolver;
  10. protected ProducerSolver $producerSolver;
  11. protected UserSolver $userSolver;
  12. public function loadDependencies(): void
  13. {
  14. $this->documentSolver = $this->loadService(DocumentSolver::class);
  15. $this->producerSolver = $this->loadService(ProducerSolver::class);
  16. $this->userSolver = $this->loadService(UserSolver::class);
  17. }
  18. public function generateReference(DocumentInterface $document): void
  19. {
  20. $class = $this->documentSolver->getClass($document);
  21. $classComplete = $this->documentSolver->getClass($document, true);
  22. $classLower = strtolower($class);
  23. if ($classLower == 'deliverynote') {
  24. $classLower = 'delivery_note';
  25. }
  26. $prefix = $this->producerSolver->getConfig('document_' . $classLower . '_prefix');
  27. $oneDocumentExist = $classComplete::searchOne(['status' => Document::STATUS_VALID], ['orderby' => 'reference_number DESC']);
  28. if ($oneDocumentExist) {
  29. $referenceNumberDocumentExist = $oneDocumentExist->reference_number;
  30. $referenceNumber = $referenceNumberDocumentExist + 1;
  31. $reference = $this->buildReference($prefix, $referenceNumber, $document);
  32. } else {
  33. $firstReference = $this->producerSolver->getConfig('document_' . $classLower . '_first_reference');
  34. if (strlen($firstReference) > 0) {
  35. $referenceNumber = (int) filter_var(str_replace('-', '', $firstReference), FILTER_SANITIZE_NUMBER_INT);
  36. $reference = $firstReference;
  37. } else {
  38. $referenceNumber = 1;
  39. $reference = $this->buildReference($prefix, 1, $document);
  40. }
  41. }
  42. $document->reference = $reference;
  43. $document->reference_number = $referenceNumber;
  44. }
  45. public function buildReference(string $prefix, int $referenceNumber, DocumentInterface $document)
  46. {
  47. $prefix = $this->replaceKeywords($prefix, $document);
  48. return $prefix . $this->strPadReference($referenceNumber);
  49. }
  50. public function replaceKeywords(string $prefix, DocumentInterface $document): string
  51. {
  52. $prefix = str_replace('[ANNEE]', date('Y'), $prefix);
  53. $prefix = str_replace('[CLIENT]', strtoupper($this->getMainName($document->user)), $prefix);
  54. return $prefix;
  55. }
  56. public function strPadReference(int $referenceNumber): string
  57. {
  58. return str_pad($referenceNumber, 5, '0', STR_PAD_LEFT);
  59. }
  60. private function getMainName(User $user): string
  61. {
  62. if($this->userSolver->isTypeLegalPerson($user) && strlen($user->name_legal_person)) {
  63. return $user->name_legal_person;
  64. }
  65. else {
  66. return $user->lastname;
  67. }
  68. }
  69. }