'jegy_partitioned', 'active' => 'jegy_active', 'inactive' => 'jegy_inactive', 'null' => 'jegy_null', ]; public function __construct() { global $db, $user; $this->db = $db; $this->user = $user; } /** * */ public function generate_hash(int $length = 10):string { // $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.'0123456789`-=~!@#$%^&*()_+,./<>?;:[]{}\|'; $_alphaSmall = 'abcdefghijklmnopqrstuvwxyz'; // small letters $_alphaCaps = strtoupper($_alphaSmall); // CAPITAL LETTERS $_numerics = '1234567890'; // numerics // $_specialChars = '`~!@#$%^&*()-_=+]}[{;:,<.>/?\'"\|'; // Special Characters $_specialChars = '~!@#$%^&*()_=+]}[{;:<.>/?|'; // Special Characters $rand = []; $rand[] = rand(1,2); //specs 1-2 $rand[] = rand(1, (int)(($length - $rand[0])/3) ); //nums $rand[] = rand(1, (int)(($length - $rand[0] - $rand[1])/2) ); //capitals $passArr = [ substr(str_shuffle($_specialChars), 0, $rand[0]), substr(str_shuffle($_numerics), 0, $rand[1]), substr(str_shuffle($_alphaCaps), 0, $rand[2]), substr(str_shuffle($_alphaSmall), 0, $length - $rand[0] - $rand[1] - $rand[2]), ]; $hash = substr(str_shuffle(implode("",$passArr)), 0, $length); return $hash; } public function billTrigger(array $details):bool { $return = false; // return $return; list($action, $this->bill, $user, $langs, $conf) = $details; if(in_array($action,['BILL_UNPAYED','BILL_MODIFY','BILL_CANCEL','BILL_UNPAYED','BILL_UNVALIDATE'])){ //inactivate all foreach($this->bill->lines as $k=>$line){ $this->setStatus($line->id); } } // else if($action == 'BILL_VALIDATE'){ //gen jegyz! // print_r($details);exit; $add2Db = true; // if($add2Db){ // $this->db->begin(); // } if(count($this->bill->lines) == 1){ //... //invoice with qr code can have only 1 item!!! foreach($this->bill->lines as $k=>$line){ if($line->fk_product){ //set all prev jegyz to inactive! $this->setStatus($line->id); //gen new jegyz & qr code $this->getQR($line->fk_product,$line->id, $add2Db); } } } // if($add2Db){ // $this->db->commit(); // } $bill = new Facture($this->db); // $bill = clone $this->bill; if ($bill->fetch($this->bill->id) > 0) { if(isset($this->qr)){ $bill->array_options['options_qr_code'] = implode(",",$this->qr); } $bill->update($user); } // $this->bill->validate($user, ''); $return = true; // exit; } elseif($action == 'BILL_PAYED'){ //activate jegyz foreach($this->bill->lines as $k=>$line){ $this->setStatus($line->id,'fk_facturedet', 'TRUE'); } } return $return; } protected function loadProduct(int $fk_product): bool { if(!isset($this->product)){ $this->product = []; } if(!isset($this->product[''.$fk_product])){ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; $this->product[''.$fk_product] = new Product($this->db); if($result = $this->product[''.$fk_product]->fetch($fk_product)){ return true; } } return false; } protected function getQR(int $fk_product,int $fk_facturedet,bool $add2Db=false): bool { if(isset($this->product[''.$fk_product])){ //exit from loop! return false; } $this->loadProduct($fk_product); //is the item an kit? $this->product[''.$fk_product]->get_sousproduits_arbo(); if($kit=$this->product[''.$fk_product]->get_arbo_each_prod()){ foreach($kit as $k=>$v){ $this->getQR($v['id'],$fk_facturedet,$add2Db); } } else{ $this->jegyz[''.$fk_product] = [ 'fk_facture' => $this->bill->id, 'fk_facturedet' => $fk_facturedet, 'fk_product' => $fk_product, 'hash' => $this->generate_hash(), //... // 'product' => $this->product[''.$fk_product], ]; if($add2Db){ if($this->jegyz[''.$fk_product]['code'] = $this->insertJegy($this->jegyz[''.$fk_product])){ $this->qr[] = $this->jegyz[''.$fk_product]['code']; } } } return true; } protected function setStatus(int $id, string $by='fk_facturedet', string $active='FALSE'):bool{ if(in_array($active,['TRUE','FALSE','NULL']) && in_array($by,['fk_facturedet','rowid'])){ $table = $this->table['partitioned']; if($by == 'fk_facturedet'){ } elseif($by == 'rowid'){ $table = $this->table['jegy_active']; $table .= ", ".$this->table['jegy_null']; if($active != 'FALSE'){ //do we need to reset an already inactiveated jegy? hmm?! maybe not. $table .= ", ".$this->table['jegy_inactive']; // 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... } } $sql = "UPDATE ".$this->db->prefix().$table." SET "; $sql .= " active = ".$this->db->escape($active).""; $sql .= ", updated_at = CURRENT_TIMESTAMP "; $sql .= " WHERE ".$this->db->escape($by)." = ".$this->db->escape($id).""; if($by == 'fk_facturedet'){ $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 } $sql .= " RETURNING rowid "; $result = $this->db->query($sql); if ($result) { return true; } } return false; } protected function insertJegy(array $jegy):?string{ $code = ''; $code .= $jegy['hash'].'-'.$jegy['fk_facturedet'].'-'; $sql = "INSERT INTO ".$this->db->prefix().$this->table['partitioned']." ("; $sql .= 'hash,active,fk_facturedet,fk_product,created_at'; // $sql .= ',type'; $sql .= ") VALUES ("; $sql .= " '".$this->db->escape($jegy['hash'])."'"; $sql .= ", NULL"; //!!! $sql .= ", ".$this->db->escape($jegy['fk_facturedet']).""; $sql .= ", ".$this->db->escape($jegy['fk_product']).""; $sql .= ", CURRENT_TIMESTAMP"; // $sql .= ", '".$this->db->escape($jegy['fk_type'])."'"; $sql .= ");"; dol_syslog(get_class($this)."::insertJegy", LOG_DEBUG); $result = $this->db->query($sql); if ($result) { $id = $this->db->last_insert_id($this->db->prefix().$this->table['partitioned']); $code .= $id; } return $code; } } ?>