jegy.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. // require_once DOL_DOCUMENT_ROOT.'//class/.class.php';
  3. // require_once DOL_DOCUMENT_ROOT.'//class/.class.php';
  4. // require_once DOL_DOCUMENT_ROOT.'//class/.class.php';
  5. class Jegy
  6. {
  7. private $user;
  8. private $db;
  9. private $qr;
  10. private $table = [
  11. 'partitioned' => 'jegy_partitioned',
  12. 'active' => 'jegy_active',
  13. 'inactive' => 'jegy_inactive',
  14. 'null' => 'jegy_null',
  15. ];
  16. public function __construct()
  17. {
  18. global $db, $user;
  19. $this->db = $db;
  20. $this->user = $user;
  21. }
  22. /**
  23. *
  24. */
  25. public function generate_hash(int $length = 10):string {
  26. // $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.'0123456789`-=~!@#$%^&*()_+,./<>?;:[]{}\|';
  27. $_alphaSmall = 'abcdefghijklmnopqrstuvwxyz'; // small letters
  28. $_alphaCaps = strtoupper($_alphaSmall); // CAPITAL LETTERS
  29. $_numerics = '1234567890'; // numerics
  30. // $_specialChars = '`~!@#$%^&*()-_=+]}[{;:,<.>/?\'"\|'; // Special Characters
  31. $_specialChars = '~!@#$%^&*()_=+]}[{;:<.>/?|'; // Special Characters
  32. $rand = [];
  33. $rand[] = rand(1,2); //specs 1-2
  34. $rand[] = rand(1, (int)(($length - $rand[0])/3) ); //nums
  35. $rand[] = rand(1, (int)(($length - $rand[0] - $rand[1])/2) ); //capitals
  36. $passArr = [
  37. substr(str_shuffle($_specialChars), 0, $rand[0]),
  38. substr(str_shuffle($_numerics), 0, $rand[1]),
  39. substr(str_shuffle($_alphaCaps), 0, $rand[2]),
  40. substr(str_shuffle($_alphaSmall), 0, $length - $rand[0] - $rand[1] - $rand[2]),
  41. ];
  42. $hash = substr(str_shuffle(implode("",$passArr)), 0, $length);
  43. return $hash;
  44. }
  45. public function billTrigger(array $details):bool
  46. {
  47. $return = false;
  48. // return $return;
  49. list($action, $this->bill, $user, $langs, $conf) = $details;
  50. if(in_array($action,['BILL_UNPAYED','BILL_MODIFY','BILL_CANCEL','BILL_UNPAYED','BILL_UNVALIDATE'])){
  51. //inactivate all
  52. foreach($this->bill->lines as $k=>$line){
  53. $this->setStatus($line->id);
  54. }
  55. }
  56. // else
  57. if($action == 'BILL_VALIDATE'){
  58. //gen jegyz!
  59. // print_r($details);exit;
  60. $add2Db = true;
  61. // if($add2Db){
  62. // $this->db->begin();
  63. // }
  64. if(count($this->bill->lines) == 1){ //...
  65. //invoice with qr code can have only 1 item!!!
  66. foreach($this->bill->lines as $k=>$line){
  67. if($line->fk_product){
  68. //set all prev jegyz to inactive!
  69. $this->setStatus($line->id);
  70. //gen new jegyz & qr code
  71. $this->getQR($line->fk_product,$line->id, $add2Db);
  72. }
  73. }
  74. }
  75. // if($add2Db){
  76. // $this->db->commit();
  77. // }
  78. $bill = new Facture($this->db);
  79. // $bill = clone $this->bill;
  80. if ($bill->fetch($this->bill->id) > 0) {
  81. if(isset($this->qr)){
  82. $bill->array_options['options_qr_code'] = implode(",",$this->qr);
  83. }
  84. $bill->update($user);
  85. }
  86. // $this->bill->validate($user, '');
  87. $return = true;
  88. // exit;
  89. }
  90. elseif($action == 'BILL_PAYED'){
  91. //activate jegyz
  92. foreach($this->bill->lines as $k=>$line){
  93. $this->setStatus($line->id,'fk_facturedet', 'TRUE');
  94. }
  95. }
  96. return $return;
  97. }
  98. protected function loadProduct(int $fk_product): bool
  99. {
  100. if(!isset($this->product)){
  101. $this->product = [];
  102. }
  103. if(!isset($this->product[''.$fk_product])){
  104. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  105. $this->product[''.$fk_product] = new Product($this->db);
  106. if($result = $this->product[''.$fk_product]->fetch($fk_product)){
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. protected function getQR(int $fk_product,int $fk_facturedet,bool $add2Db=false): bool
  113. {
  114. if(isset($this->product[''.$fk_product])){
  115. //exit from loop!
  116. return false;
  117. }
  118. $this->loadProduct($fk_product);
  119. //is the item an kit?
  120. $this->product[''.$fk_product]->get_sousproduits_arbo();
  121. if($kit=$this->product[''.$fk_product]->get_arbo_each_prod()){
  122. foreach($kit as $k=>$v){
  123. $this->getQR($v['id'],$fk_facturedet,$add2Db);
  124. }
  125. }
  126. else{
  127. $this->jegyz[''.$fk_product] = [
  128. 'fk_facture' => $this->bill->id,
  129. 'fk_facturedet' => $fk_facturedet,
  130. 'fk_product' => $fk_product,
  131. 'hash' => $this->generate_hash(),
  132. //...
  133. // 'product' => $this->product[''.$fk_product],
  134. ];
  135. if($add2Db){
  136. if($this->jegyz[''.$fk_product]['code'] = $this->insertJegy($this->jegyz[''.$fk_product])){
  137. $this->qr[] = $this->jegyz[''.$fk_product]['code'];
  138. }
  139. }
  140. }
  141. return true;
  142. }
  143. protected function setStatus(int $id, string $by='fk_facturedet', string $active='FALSE'):bool{
  144. if(in_array($active,['TRUE','FALSE','NULL']) && in_array($by,['fk_facturedet','rowid'])){
  145. $table = $this->table['partitioned'];
  146. if($by == 'fk_facturedet'){
  147. }
  148. elseif($by == 'rowid'){
  149. $table = $this->table['jegy_active'];
  150. $table .= ", ".$this->table['jegy_null'];
  151. if($active != 'FALSE'){
  152. //do we need to reset an already inactiveated jegy? hmm?! maybe not.
  153. $table .= ", ".$this->table['jegy_inactive'];
  154. // or if we could not find in active or null should chk in inactive too (or we do already this?! todo: chk pg docs...) :S or reset by fk_facturedet...
  155. }
  156. }
  157. $sql = "UPDATE ".$this->db->prefix().$table." SET ";
  158. $sql .= " active = ".$this->db->escape($active)."";
  159. $sql .= ", updated_at = CURRENT_TIMESTAMP ";
  160. $sql .= " WHERE ".$this->db->escape($by)." = ".$this->db->escape($id)."";
  161. if($by == 'fk_facturedet'){
  162. $sql .= " AND (active = TRUE OR active IS NULL) "; // <-- !!! becouse it can be a lot of inactivated jegyz for the particular bill... and cant decide wichone to update :S so src only in active and unpaid(==null). ->it works with rowid
  163. }
  164. $sql .= " RETURNING rowid ";
  165. $result = $this->db->query($sql);
  166. if ($result) {
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. protected function insertJegy(array $jegy):?string{
  173. $code = '';
  174. $code .= $jegy['hash'].'-'.$jegy['fk_facturedet'].'-';
  175. $sql = "INSERT INTO ".$this->db->prefix().$this->table['partitioned']." (";
  176. $sql .= 'hash,active,fk_facturedet,fk_product,created_at';
  177. // $sql .= ',type';
  178. $sql .= ") VALUES (";
  179. $sql .= " '".$this->db->escape($jegy['hash'])."'";
  180. $sql .= ", NULL"; //!!!
  181. $sql .= ", ".$this->db->escape($jegy['fk_facturedet'])."";
  182. $sql .= ", ".$this->db->escape($jegy['fk_product'])."";
  183. $sql .= ", CURRENT_TIMESTAMP";
  184. // $sql .= ", '".$this->db->escape($jegy['fk_type'])."'";
  185. $sql .= ");";
  186. dol_syslog(get_class($this)."::insertJegy", LOG_DEBUG);
  187. $result = $this->db->query($sql);
  188. if ($result) {
  189. $id = $this->db->last_insert_id($this->db->prefix().$this->table['partitioned']);
  190. $code .= $id;
  191. }
  192. return $code;
  193. }
  194. }
  195. ?>