You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

m231018_074957_add_column_document_reference_number.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. use common\logic\Document\DeliveryNote\Module\DeliveryNoteModule;
  3. use common\logic\Document\Invoice\Module\InvoiceModule;
  4. use common\logic\Document\Quotation\Module\QuotationModule;
  5. use yii\db\Migration;
  6. use yii\db\Schema;
  7. /**
  8. * Class m231018_074957_add_column_document_reference_number
  9. */
  10. class m231018_074957_add_column_document_reference_number extends Migration
  11. {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function safeUp()
  16. {
  17. $this->addColumn('quotation', 'reference_number', Schema::TYPE_INTEGER);
  18. $this->addColumn('delivery_note', 'reference_number', Schema::TYPE_INTEGER);
  19. $this->addColumn('invoice', 'reference_number', Schema::TYPE_INTEGER);
  20. $quotationArray = QuotationModule::getInstance()->getRepository()->findAll();
  21. $deliveryNoteArray = DeliveryNoteModule::getInstance()->getRepository()->findAll();
  22. $invoiceArray = InvoiceModule::getInstance()->getRepository()->findAll();
  23. $this->updateReferenceNumber($quotationArray);
  24. $this->updateReferenceNumber($deliveryNoteArray);
  25. $this->updateReferenceNumber($invoiceArray);
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function safeDown()
  31. {
  32. $this->dropColumn('quotation', 'reference_number');
  33. $this->dropColumn('delivery_note', 'reference_number');
  34. $this->dropColumn('invoice', 'reference_number');
  35. }
  36. public function updateReferenceNumber(array $documentArray)
  37. {
  38. foreach($documentArray as $document) {
  39. $document->reference_number = $this->getReferenceNumber($document);
  40. $document->save();
  41. }
  42. }
  43. public function getReferenceNumber($document)
  44. {
  45. $referenceDocument = $document->reference;
  46. $pattern = '#([A-Z]+)?([0-9]+)#';
  47. preg_match($pattern, $referenceDocument, $matches, PREG_OFFSET_CAPTURE);
  48. if(isset($matches[2])) {
  49. return (int) $matches[2][0];
  50. }
  51. return null;
  52. }
  53. }