Distinta.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * @author Open Mind
  4. * @date 13/04/2018
  5. * @time 11:43
  6. */
  7. namespace Modules\Dimatec;
  8. use OpenMind\FindBox\QueueCommand;
  9. use OpenMind\System\QueueSystem;
  10. class Distinta
  11. {
  12. /** @var \DB\SQL $dbHandle */
  13. protected $dbHandle;
  14. /** @var int $userId */
  15. protected $userId;
  16. public function __construct()
  17. {
  18. global $f3;
  19. $this->dbHandle = $f3->get('DB');
  20. $this->userId = $f3->get('USER')->id;
  21. }
  22. /**
  23. * Delete from table all commesse present and unbind all Prebolle to ESL
  24. */
  25. public function TrashCommesse()
  26. {
  27. $this->dbHandle->exec("DELETE FROM `label_bindings` WHERE barcode IN (SELECT commessa FROM distinte WHERE idUtente={$this->userId} AND deleted=0)");
  28. $this->dbHandle->exec("DELETE FROM `distinte` WHERE idUtente={$this->userId} AND deleted=0");
  29. }
  30. /**
  31. * Virtual delete and article of a commessa by set deleted=1 in database field
  32. *
  33. * @param string $article
  34. */
  35. public function DeleteCommessaArticle(string $article, string $commessa)
  36. {
  37. $this->dbHandle->exec("UPDATE `distinte` SET deleted=1 WHERE commessa='{$commessa}' AND barcode='{$article}' AND idUtente={$this->userId} AND deleted=0");
  38. }
  39. /**
  40. * Check if commessa isn't deleted and present
  41. *
  42. * @param string $barcode
  43. * @return bool
  44. */
  45. public function commessaExists(string $barcode): bool
  46. {
  47. return (count($this->dbHandle->exec("SELECT * FROM `distinte` WHERE `commessa` = '{$barcode}' AND `idUtente` = '{$this->userId}' AND `deleted` = '0'")) > 0);
  48. }
  49. /**
  50. * Ottiene il codice articolo da ogni tipo di campo 'utile'
  51. *
  52. * @param string $id
  53. * @return null|string Codice Articolo
  54. */
  55. public function GetBarcode(string $id): ?string
  56. {
  57. $binding = $this->dbHandle->exec("SELECT barcode FROM `distinte` WHERE `barcode` = '{$id}' OR `codcliente` = '{$id}' OR `commessa` = '{$id}'");
  58. if (count($binding) > 0)
  59. return $binding[0]['barcode'];
  60. return null;
  61. }
  62. /**
  63. * Retrieve all commesse associated to a Product
  64. *
  65. * @param string $barcode
  66. * @return array
  67. */
  68. public function getCommesseFromProduct(string $barcode): array
  69. {
  70. return $this->dbHandle->exec("SELECT * FROM `distinte` WHERE `barcode` = '{$barcode}' AND `idUtente` = '{$this->userId}' AND `deleted` = '0' ORDER BY `commessa`");
  71. }
  72. public function UnbindAll()
  73. {
  74. global $f3;
  75. $labels = $this->getListeBinded();
  76. foreach($labels as $label) {
  77. $msgDisplay = new QueueCommand($label['label_id']);
  78. $msgDisplay->setDisplay($f3->get("LABELS.TEMPLATES.DEFAULT"), $label['label_id']);
  79. $msgReset = new QueueCommand($label['label_id']);
  80. $msgReset->saveProducts('', [
  81. 'Q.ta' => '',
  82. 'Commessa' => $label['label_id']
  83. ]);
  84. $qm = new QueueSystem('FindBoxService');
  85. $qm->send($msgReset);
  86. $qm->send($msgDisplay);
  87. }
  88. }
  89. /**
  90. * Restituisce le commesse ancora da bindare
  91. *
  92. * @return array|FALSE|int
  93. */
  94. public function getListeBinded()
  95. {
  96. return $this->dbHandle->exec("SELECT label_id FROM label_bindings WHERE barcode IN (SELECT commessa FROM `distinte` WHERE deleted=0 AND idutente={$this->userId})");
  97. }
  98. /**
  99. * Restituisce le commesse ancora da bindare
  100. *
  101. * @return array|FALSE|int
  102. */
  103. public function getListeToBind()
  104. {
  105. return $this->dbHandle->exec("SELECT DISTINCT commessa FROM `distinte` WHERE deleted=0 and idutente={$this->userId} AND commessa NOT IN (SELECT barcode FROM label_bindings) ORDER BY commessa");
  106. }
  107. public function getDistinta(string $maxRows = '3')
  108. {
  109. return $this->dbHandle->exec("SELECT * FROM distinta_esplosa WHERE idutente={$this->userId} AND qta_prelevare>0 and anomalie=0 ORDER BY sequenza LIMIT {$maxRows}");
  110. }
  111. /**
  112. * @param string $barcode
  113. * @return array
  114. */
  115. public function MarkBarcodeAsAnomalia(string $barcode): array
  116. {
  117. global $f3;
  118. $CORE_SETTINGS = $f3->get('CORE.fb');
  119. $findBoxCore = new FindBoxCoreSystem(...array_values($CORE_SETTINGS));
  120. $lblId = $this->GetLabelIdFromBarcode($barcode);
  121. $label = new SmartESL($findBoxCore, [$lblId]);
  122. $label->BlinkSpecial(3, 1, 1000, 10);
  123. $sql = "UPDATE distinta_esplosa d JOIN label_bindings b ON d.barcode = b.barcode SET anomalie=1 WHERE idutente={$this->userId} AND b.barcode='{$barcode}'";
  124. return $this->dbHandle->exec($sql);
  125. }
  126. /**
  127. * Get quantity for article binded to label
  128. *
  129. * @param string $barcode
  130. * @return int
  131. */
  132. public function getArticleCount(string $barcode): int
  133. {
  134. $sql = "SELECT d.qta_prelevare from distinta d JOIN label_bindings b ON d.barcode = b.barcode WHERE idutente={$this->userId} AND b.barcode='{$barcode}'";
  135. $record = $this->dbHandle->exec($sql);
  136. if (count($record) > 0)
  137. return $record[0]['qta_prelevare'];
  138. return 0;
  139. }
  140. /**
  141. * @param string $barcode
  142. * @return null|string
  143. */
  144. public function GetLabelIdFromBarcode(string $barcode): ?string
  145. {
  146. $label = $this->dbHandle->exec("SELECT label_id FROM `label_bindings` WHERE barcode='{$barcode}'");
  147. if (count($label) > 0)
  148. return $label[0]['label_id'];
  149. return null;
  150. }
  151. }