Browse Source

Fixes: Bug Critique + Interface Triggers

Ghislain Loaec 5 years ago
parent
commit
0dc35a5d42

+ 34
- 10
class/phcp.class.php View File

@@ -102,6 +102,31 @@ class Phcp extends CommonObject
102 102
       return $list;
103 103
     }
104 104
 
105
+    function by_societe($socid){
106
+      global $conf, $langs;
107
+      $sql = "SELECT";
108
+		  $sql.= " p.rowid";
109
+      $sql.= " FROM ".MAIN_DB_PREFIX."phcp as p";
110
+      $sql.= " WHERE p.fk_societe = '".$socid."'";
111
+ 
112
+      dol_syslog(get_class($this)."::all sql=".$sql, LOG_DEBUG);
113
+      $resql=$this->db->query($sql);
114
+      if ($resql){
115
+        if ($num = $this->db->num_rows($resql)){
116
+          $i = 0;
117
+          while ($i < $num){
118
+            $obj = $this->db->fetch_object($resql);
119
+            if ($obj){
120
+              $phcp = new Phcp($this->db);
121
+              $phcp->fetch($obj->rowid);
122
+              $list[]= $phcp;
123
+            }
124
+            $i++;
125
+          }
126
+        }
127
+      }
128
+      return $list;
129
+    }
105 130
 
106 131
     function no_societe(){
107 132
       global $conf, $langs;
@@ -163,15 +188,14 @@ class Phcp extends CommonObject
163 188
 		// Put here code to add control on parameters values
164 189
 
165 190
         // Insert request
166
-		$sql = "INSERT INTO ".MAIN_DB_PREFIX."phcp(";
191
+		$sql = "INSERT INTO ".MAIN_DB_PREFIX."phcp (";
167 192
 		
168 193
 		$sql.= "nom,";
169 194
 		$sql.= "ref,";
170 195
 		$sql.= "fk_phcp_type,";
171 196
 		$sql.= "fk_societe";
172 197
 
173
-		
174
-        $sql.= ") VALUES (";
198
+    $sql.= ") VALUES (";
175 199
         
176 200
 		$sql.= " ".(! isset($this->nom)?'NULL':"'".$this->db->escape($this->nom)."'").",";
177 201
 		$sql.= " ".(! isset($this->ref)?'NULL':"'".$this->db->escape($this->ref)."'").",";
@@ -187,14 +211,14 @@ class Phcp extends CommonObject
187 211
     	if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
188 212
 
189 213
 		if (! $error)
190
-        {
214
+    {
191 215
             $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."phcp");
216
+            
192 217
             foreach($this->phcp_type->products as $product){
193 218
               $sql = "INSERT INTO ";
194 219
               $sql.= MAIN_DB_PREFIX."compteur (";
195
-              $sql.= "fk_phcp, fk_product, compteur, last_compteur";
196
-              $sql.= "VALUES ('".$this->id."','".$product->id."', '0', '0')";
197
-		          $this->db->begin();
220
+              $sql.= "fk_phcp, fk_product, compteur, last_compteur)";
221
+              $sql.= " VALUES ('".$this->db->escape($this->id)."','".$this->db->escape($product->id)."', '0', '0')";
198 222
 
199 223
 	            dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
200 224
               $resql=$this->db->query($sql);
@@ -222,15 +246,15 @@ class Phcp extends CommonObject
222 246
 			{
223 247
 	            dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
224 248
 	            $this->error.=($this->error?', '.$errmsg:$errmsg);
225
-			}
249
+      }
226 250
 			$this->db->rollback();
227 251
 			return -1*$error;
228 252
 		}
229 253
 		else
230
-		{
254
+    {
231 255
 			$this->db->commit();
232 256
             return $this->id;
233
-		}
257
+    }
234 258
     }
235 259
 
236 260
 

core/triggers/interface_99_modMyModule_Mytrigger.class.php → core/triggers/interface_90_modPhotocopies_Photocopiestriggers.class.php View File

@@ -33,7 +33,10 @@
33 33
 /**
34 34
  * Trigger class
35 35
  */
36
-class InterfaceMytrigger
36
+require_once(DOL_DOCUMENT_ROOT."/photocopies/class/phcp.class.php");
37
+
38
+
39
+class InterfacePhotocopiestriggers
37 40
 {
38 41
 
39 42
     private $db;
@@ -189,9 +192,18 @@ class InterfaceMytrigger
189 192
                 "Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id
190 193
             );
191 194
         } elseif ($action == 'COMPANY_DELETE') {
195
+                exit;
196
+            echo "Trigger ".$object->id/"<br>";
197
+            $phcps = new Phcp($db);
198
+            $phcps->by_societe($object->id); 
199
+            foreach($phcps as $phcp){
200
+              echo "Delete phcp ".$phcp->id."<br>";
201
+              $phcp->delete($user);
202
+            }
192 203
             dol_syslog(
193 204
                 "Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id
194 205
             );
206
+
195 207
         }
196 208
 
197 209
         // Contacts

+ 2
- 1
sql/llx_phcp.key.sql View File

@@ -21,4 +21,5 @@ ALTER TABLE llx_phcp ADD INDEX idx_phcp_fk_societe (fk_societe);
21 21
 
22 22
 ALTER TABLE llx_phcp ADD CONSTRAINT fk_phcp_fk_phcp_type FOREIGN KEY (fk_phcp_type) REFERENCES llx_phcp_type (rowid);
23 23
 -- ON DELETE CASCADE ON UPDATE CASCADE
24
-ALTER TABLE llx_phcp ADD CONSTRAINT fk_phcp_fk_societe FOREIGN KEY (fk_societe) REFERENCES llx_societe (rowid);
24
+ALTER TABLE llx_phcp ADD CONSTRAINT fk_phcp_fk_societe FOREIGN KEY (fk_societe) REFERENCES llx_societe (rowid) 
25
+ON DELETE CASCADE ON UPDATE CASCADE;