dolibarr /htdocs/compta/sociales/class/chargesociales.class.php

Language PHP Lines 424
MD5 Hash 5d46e954c5c61d6f6d3cb887db4d39af Estimated Cost $7,125 (why?)
Repository https://github.com/asterix14/dolibarr.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?php
/* Copyright (C) 2002      Rodolphe Quiedeville <rodolphe@quiedeville.org>
 * Copyright (C) 2004-2007 Laurent Destailleur  <eldy@users.sourceforge.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *      \file       htdocs/compta/sociales/class/chargesociales.class.php
 *		\ingroup    facture
 *		\brief      Fichier de la classe des charges sociales
 */

require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");


/**     \class      ChargeSociales
 *		\brief      Classe permettant la gestion des paiements des charges
 *                  La tva collectee n'est calculee que sur les factures payees.
 */
class ChargeSociales extends CommonObject
{
	public $element='rowid';
    public $table='chargesociales';
	public $table_element='chargesociales';

	var $id;
	var $date_ech;
	var $lib;
	var $type;
	var $type_libelle;
	var $amount;
	var $paye;
	var $periode;


	function ChargeSociales($DB)
	{
		$this->db = $DB;

		return 1;
	}

	/**
	 *   \brief      Retrouve et charge une charge sociale
	 *   \return     int     1 si trouve, 0 sinon
	 */
	function fetch($id)
	{
		$sql = "SELECT cs.rowid, cs.date_ech,";
		$sql.= " cs.libelle as lib, cs.fk_type, cs.amount, cs.paye, cs.periode,";
		$sql.= " c.libelle";
		$sql.= " FROM ".MAIN_DB_PREFIX."chargesociales as cs, ".MAIN_DB_PREFIX."c_chargesociales as c";
		$sql.= " WHERE cs.fk_type = c.id";
		$sql.= " AND cs.rowid = ".$id;

		dol_syslog("ChargesSociales::fetch sql=".$sql);
		$resql=$this->db->query($sql);
		if ($resql)
		{
			if ($this->db->num_rows($resql))
			{
				$obj = $this->db->fetch_object($resql);

				$this->id             = $obj->rowid;
				$this->ref            = $obj->rowid;
				$this->date_ech       = $this->db->jdate($obj->date_ech);
				$this->lib            = $obj->lib;
				$this->type           = $obj->fk_type;
				$this->type_libelle   = $obj->libelle;
				$this->amount         = $obj->amount;
				$this->paye           = $obj->paye;
				$this->periode        = $this->db->jdate($obj->periode);

				return 1;
			}
			else
			{
				return 0;
			}
			$this->db->free($resql);
		}
		else
		{
			$this->error=$this->db->error();
			return -1;
		}
	}


	/**
	 *      \brief      Create a social contribution in database
	 *      \param      user    User making creation
	 *      \return     int     <0 if KO, id if OK
	 */
	function create($user)
	{
		// Nettoyage parametres
		$newamount=price2num($this->amount,'MT');

		// Validation parametres
		if (! $newamount > 0)
		{
			$this->error="ErrorBadParameter";
			return -2;
		}

		$this->db->begin();

		$sql = "INSERT INTO ".MAIN_DB_PREFIX."chargesociales (fk_type, libelle, date_ech, periode, amount)";
		$sql.= " VALUES (".$this->type.",'".$this->db->escape($this->lib)."',";
		$sql.= " '".$this->db->idate($this->date_ech)."','".$this->db->idate($this->periode)."',";
		$sql.= " ".price2num($newamount);
		$sql.= ")";

		dol_syslog("ChargesSociales::create sql=".$sql);
		$resql=$this->db->query($sql);
		if ($resql)
		{
			$this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."chargesociales");

			//dol_syslog("ChargesSociales::create this->id=".$this->id);
			$this->db->commit();
			return $this->id;
		}
		else
		{
			$this->error=$this->db->error();
			$this->db->rollback();
			return -1;
		}
	}


	/**
	 *      Delete a social contribution
	 *      @param      user    Object user making delete
	 *      @return     int     <0 if KO, >0 if OK
	 */
	function delete($user)
	{
	    $error=0;

	    $this->db->begin();

	    // Get bank transaction lines for this social contributions
	    include_once(DOL_DOCUMENT_ROOT."/compta/bank/class/account.class.php");
	    $account=new Account($this->db);
        $lines_url=$account->get_url('',$this->id,'sc');

        // Delete bank urls
        foreach ($lines_url as $line_url)
        {
            if (! $error)
            {
                $accountline=new AccountLine($this->db);
                $accountline->fetch($line_url['fk_bank']);
                $result=$accountline->delete_urls($user);
                if ($result < 0)
                {
                    $error++;
                }
            }
        }

        // Delete payments
        if (! $error)
        {
    	    $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge where fk_charge='".$this->id."'";
    		dol_syslog(get_class($this)."::delete sql=".$sql);
    		$resql=$this->db->query($sql);
    		if (! $resql)
    		{
    		    $error++;
    			$this->error=$this->db->lasterror();
    		}
        }

        if (! $error)
        {
            $sql = "DELETE FROM ".MAIN_DB_PREFIX."chargesociales where rowid='".$this->id."'";
    		dol_syslog(get_class($this)."::delete sql=".$sql);
    		$resql=$this->db->query($sql);
    		if (! $resql)
    		{
    		    $error++;
    			$this->error=$this->db->lasterror();
    		}
        }

		if (! $error)
		{
		    $this->db->commit();
			return 1;
		}
		else
		{
		    $this->db->rollback();
			return -1;
		}

	}


	/**
	 *      Met a jour une charge sociale
	 *      @param      user    Utilisateur qui modifie
	 *      @return     int     <0 si erreur, >0 si ok
	 */
	function update($user)
	{
		$this->db->begin();

		$sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales";
		$sql.= " SET libelle='".$this->db->escape($this->lib)."',";
		$sql.= " date_ech='".$this->db->idate($this->date_ech)."',";
		$sql.= " periode='".$this->db->idate($this->periode)."'";
		$sql.= " WHERE rowid=".$this->id;

		dol_syslog("ChargesSociales::update sql=".$sql);
		$resql=$this->db->query($sql);
		if ($resql)
		{
			$this->db->commit();
			return 1;
		}
		else
		{
			$this->error=$this->db->error();
			$this->db->rollback();
			return -1;
		}
	}


	function solde($year = 0)
	{
		$sql = "SELECT sum(f.amount) as amount";
		$sql .= " FROM ".MAIN_DB_PREFIX."chargesociales as f WHERE paye = 0";

		if ($year) {
			$sql .= " AND f.datev >= '$y-01-01' AND f.datev <= '$y-12-31' ";
		}

		$result = $this->db->query($sql);
		if ($result) {
			if ($this->db->num_rows($result)) {
				$obj = $this->db->fetch_object($result);
				return $obj->amount;
			} else {
				return 0;
			}

			$this->db->free();

		} else {
			print $this->db->error();
			return -1;
		}
	}

	/**
	 *    Tag social contribution as payed completely
	 *    @param      user         Object user making change
	 */
	function set_paid($user)
	{
		$sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales";
		$sql.= " set paye=1";
		$sql.= " WHERE rowid = ".$this->id;
		$return = $this->db->query($sql);
		if ($return) return 1;
		else return -1;
	}

	/**
	 *    \brief      Retourne le libelle du statut d'une charge (impaye, payee)
	 *    \param      mode          0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long
	 *    \return     string        Libelle
	 */
	function getLibStatut($mode=0)
	{
		return $this->LibStatut($this->paye,$mode);
	}

	/**
	 *    	\brief      Renvoi le libelle d'un statut donne
	 *    	\param      statut        	Id statut
	 *    	\param      mode          	0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
	 *    	\return     string        	Libelle du statut
	 */
	function LibStatut($statut,$mode=0)
	{
		global $langs;
		$langs->load('customers');

		if ($mode == 0)
		{
			if ($statut ==  0) return $langs->trans("Unpaid");
			if ($statut ==  1) return $langs->trans("Paid");
		}
		if ($mode == 1)
		{
			if ($statut ==  0) return $langs->trans("Unpaid");
			if ($statut ==  1) return $langs->trans("Paid");
		}
		if ($mode == 2)
		{
			if ($statut ==  0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
			if ($statut ==  1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
		}
		if ($mode == 3)
		{
			if ($statut ==  0) return img_picto($langs->trans("Unpaid"), 'statut1');
			if ($statut ==  1) return img_picto($langs->trans("Paid"), 'statut6');
		}
		if ($mode == 4)
		{
			if ($statut ==  0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
			if ($statut ==  1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
		}
		if ($mode == 5)
		{
			if ($statut ==  0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
			if ($statut ==  1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
		}

		return "Error, mode/status not found";
	}


	/**
	 *    	\brief      Renvoie nom clicable (avec eventuellement le picto)
	 *		\param		withpicto		0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
	 * 		\param		maxlen			Longueur max libelle
	 *		\return		string			Chaine avec URL
	 */
	function getNomUrl($withpicto=0,$maxlen=0)
	{
		global $langs;

		$result='';

		if (empty($this->ref)) $this->ref=$this->lib;

		$lien = '<a href="'.DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$this->id.'">';
		$lienfin='</a>';

		if ($withpicto) $result.=($lien.img_object($langs->trans("ShowSocialContribution").': '.$this->lib,'bill').$lienfin.' ');
		if ($withpicto && $withpicto != 2) $result.=' ';
		if ($withpicto != 2) $result.=$lien.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$lienfin;
		return $result;
	}

	/**
	 * 	Return amount of payments already done
	 *	@return		int		Amount of payment already done, <0 if KO
	 */
	function getSommePaiement()
	{
		$table='paiementcharge';
		$field='fk_charge';

		$sql = 'SELECT sum(amount) as amount';
		$sql.= ' FROM '.MAIN_DB_PREFIX.$table;
		$sql.= ' WHERE '.$field.' = '.$this->id;

		dol_syslog("ChargeSociales::getSommePaiement sql=".$sql, LOG_DEBUG);
		$resql=$this->db->query($sql);
		if ($resql)
		{
		    $amount=0;

			$obj = $this->db->fetch_object($resql);
			if ($obj) $amount=$obj->amount?$obj->amount:0;

			$this->db->free($resql);
			return $amount;
		}
		else
		{
			return -1;
		}
	}

    /**
     *  Initialise an instance with random values.
     *  Used to build previews or test instances.
     *	id must be 0 if object instance is a specimen.
     *
     *  @return	void
     */
    function initAsSpecimen()
    {
        global $user,$langs,$conf;

        // Initialize parameters
        $this->id=0;
        $this->ref = 'SPECIMEN';
        $this->specimen=1;
        $this->paye = 0;
        $this->date = time();
        $this->date_ech=$this->date+3600*24*30;
        $this->period=$this->date+3600*24*30;
        $this->amount=100;
        $this->lib = 0;
        $this->type = 1;
        $this->type_libelle = 'Social contribution label';
    }
}

?>
Back to Top