Browse Source

Fix it was not possible to create/edit a new chart of account

Laurent Destailleur 8 years ago
parent
commit
6c91cb4f90

+ 8 - 14
htdocs/accountancy/admin/account.php

@@ -145,8 +145,6 @@ if ($action == 'delete') {
 
 $pcgver = $conf->global->CHARTOFACCOUNTS;
 
-
-
 $sql = "SELECT aa.rowid, aa.fk_pcg_version, aa.pcg_type, aa.pcg_subtype, aa.account_number, aa.account_parent , aa.label, aa.active, ";
 $sql .= " a2.rowid as rowid2, a2.label as label2, a2.account_number as account_number2";
 $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
@@ -198,10 +196,12 @@ if ($resql) {
 	if ($search_pcgsubtype != "") $params.= '&search_pcgsubtype='.urlencode($search_pcgsubtype);
     if ($optioncss != '') $param.='&optioncss='.$optioncss;
 	
-	print_barre_liste($langs->trans('ListAccounts'), $page, $_SERVER["PHP_SELF"], $params, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_accountancy');
-	
 	print '<form method="GET" action="' . $_SERVER["PHP_SELF"] . '">';
 	
+	$htmlbuttonadd = '<a class="butAction" href="./card.php?action=create">' . $langs->trans("Addanaccount") . '</a>';
+	
+    print_barre_liste($langs->trans('ListAccounts'), $page, $_SERVER["PHP_SELF"], $params, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_accountancy', 0, $htmlbuttonadd);
+	
 	// Box to select active chart of account
     $var = ! $var;
     print $langs->trans("Selectchartofaccounts") . " : ";
@@ -229,13 +229,7 @@ if ($resql) {
     print "</select>";
     print '<input type="submit" class="button" name="change_chart" value="'.dol_escape_htmltag($langs->trans("ChangeAndLoad")).'">';
     print '<br>';    
-    print "<br>\n";
-	
-	print '<a class="butAction" href="./card.php?action=create">' . $langs->trans("Addanaccount") . '</a>';
-	print '<a class="butAction" href="./categories.php">' . $langs->trans("ApplyMassCategories") . '</a>';
-	// print '<a class="butAction" href="./importaccounts.php">' . $langs->trans("ImportAccount") . '</a>';
-	// print '<a class="butAction" href="./productaccount.php">' . $langs->trans("CheckProductAccountancyCode") . '</a>';
-	print '<br><br>';
+	print '<br>';
 	
 	print '<table class="noborder" width="100%">';
 	print '<tr class="liste_titre">';
@@ -307,12 +301,12 @@ if ($resql) {
 		
 		// Action
 		print '<td align="center">';
-		if ($user->admin) {
-			print '<a href="./card.php?action=update&id=' . $obj->rowid . '">';
+		if ($user->rights->accounting->chartofaccount) {
+			print '<a href="./card.php?action=update&id=' . $obj->rowid . '&backtopage='.urlencode($_SERVER["PHP_SELF"].'?chartofaccounts='.$object->id).'">';
 			print img_edit();
 			print '</a>';
 			print '&nbsp;';
-			print '<a href="./card.php?action=delete&id=' . $obj->rowid . '">';
+			print '<a href="./card.php?action=delete&id=' . $obj->rowid . '&backtopage='.urlencode($_SERVER["PHP_SELF"].'?chartofaccounts='.$object->id). '">';
 			print img_delete();
 			print '</a>';
 		}

+ 29 - 11
htdocs/accountancy/admin/card.php

@@ -26,6 +26,7 @@
 require '../../main.inc.php';
 require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
+require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountancysystem.class.php';
 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/html.formventilation.class.php';
 require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
 
@@ -159,13 +160,15 @@ if ($action == 'add' && $user->rights->accounting->chartofaccount)
 		$result = $object->update($user);
 		
 		if ($result > 0) {
-			header("Location: " . $_SERVER["PHP_SELF"] . "?id=" . $id);
+		    $urltogo=$backtopage?$backtopage:($_SERVER["PHP_SELF"]."?id=".$id);
+		    header("Location: " . $urltogo);
 			exit();
 		} else {
 			$mesg = $object->error;
 		}
 	} else {
-		header("Location: " . $_SERVER["PHP_SELF"] . "?id=" . $id);
+	    $urltogo=$backtopage?$backtopage:($_SERVER["PHP_SELF"]."?id=".$id);
+	    header("Location: " . $urltogo);
 		exit();
 	}
 } else if ($action == 'delete' && $user->rights->accounting->chartofaccount) {
@@ -196,6 +199,9 @@ $form = new Form($db);
 $htmlacc = new FormVentilation($db);
 $formaccounting = new FormAccounting($db);
 
+$accountsystem = new AccountancySystem($db);
+$accountsystem->fetch($conf->global->CHARTOFACCOUNTS);
+
 // Create mode
 if ($action == 'create') {
 	print load_fiche_titre($langs->trans('NewAccountingAccount'));
@@ -208,36 +214,45 @@ if ($action == 'create') {
 	
 	print '<table class="border" width="100%">';
 
+	// Chart of account
+	print '<tr><td class="titlefieldcreate"><span class="fieldrequired">' . $langs->trans("Chartofaccounts") . '</span></td>';
+	print '<td>';
+	print $accountsystem->ref;
+	print '</td></tr>';
+	
+	
 	// Account number
 	print '<tr><td class="titlefieldcreate"><span class="fieldrequired">' . $langs->trans("AccountNumber") . '</span></td>';
-	print '<td><input name="account_number" size="30" value="' . $object->account_number . '"</td></tr>';
+	print '<td><input name="account_number" size="30" value="' . $object->account_number . '"></td></tr>';
 
 	// Label
 	print '<tr><td><span class="fieldrequired">' . $langs->trans("Label") . '</span></td>';
-	print '<td><input name="label" size="70" value="' . $object->label . '"</td></tr>';
+	print '<td><input name="label" size="70" value="' . $object->label . '"></td></tr>';
 
 	// Account parent
 	print '<tr><td>' . $langs->trans("Accountparent") . '</td>';
 	print '<td>';
-	print $htmlacc->select_account($object->account_parent, 'account_parent', 1);
+	print $htmlacc->select_account($object->account_parent, 'account_parent', 1, null, 0, 0, 'minwidth200');
 	print '</td></tr>';
 
 	// Category
 	print '<tr><td>' . $langs->trans("AccountingCategory") . '</td>';
 	print '<td>';
-	$formaccounting->select_accounting_category($object->account_category, 'account_category', 1);
+	$formaccounting->select_accounting_category($object->account_category, 'account_category', 1, 0, 1);
 	print '</td></tr>';
 
 	// Chart of accounts type
 	print '<tr><td>' . $langs->trans("Pcgtype") . '</td>';
 	print '<td>';
-	print $htmlacc->select_pcgtype($object->pcg_type, 'pcg_type');
+	print '<input type="text" name="pcg_type" value="'.dol_escape_htmltag(isset($_POST['pcg_type'])?GETPOST('pcg_type','alpha'):$object->pcg_type).'">';
+	//print $htmlacc->select_pcgtype($object->pcg_type, 'pcg_type', 1);
 	print '</td></tr>';
 
 	// Chart of acounts subtype
 	print '<tr><td>' . $langs->trans("Pcgsubtype") . '</td>';
 	print '<td>';
-	print $htmlacc->select_pcgsubtype($object->pcg_subtype, 'pcg_subtype');
+	print '<input type="text" name="pcg_subtype" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$object->pcg_subtype).'">';
+	//print $htmlacc->select_pcgsubtype($object->pcg_subtype, 'pcg_subtype', 1);
 	print '</td></tr>';
 	
 	print '</table>';
@@ -268,7 +283,8 @@ if ($action == 'create') {
 			print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
 			print '<input type="hidden" name="action" value="edit">';
 			print '<input type="hidden" name="id" value="' . $id . '">';
-			
+			print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
+				
 			print '<table class="border" width="100%">';
 			
 			// Account number
@@ -294,13 +310,15 @@ if ($action == 'create') {
 			// Chart of accounts type
 			print '<tr><td>' . $langs->trans("Pcgtype") . '</td>';
 			print '<td>';
-			print $htmlacc->select_pcgtype($object->pcg_type, 'pcg_type');
+			//print $htmlacc->select_pcgtype($object->pcg_type, 'pcg_type', 1);
+			print '<input type="text" name="pcg_type" value="'.dol_escape_htmltag(isset($_POST['pcg_type'])?GETPOST('pcg_type','alpha'):$object->pcg_type).'">';
 			print '</td></tr>';
 
 			// Chart of accounts subtype
 			print '<tr><td>' . $langs->trans("Pcgsubtype") . '</td>';
 			print '<td>';
-			print $htmlacc->select_pcgsubtype($object->pcg_subtype, 'pcg_subtype');
+			print '<input type="text" name="pcg_subtype" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$object->pcg_subtype).'">';
+			//print $htmlacc->select_pcgsubtype($object->pcg_subtype, 'pcg_subtype', 1);
 			print '</td></tr>';
 			
 			print '</table>';

+ 49 - 0
htdocs/accountancy/class/accountancysystem.class.php

@@ -47,6 +47,55 @@ class AccountancySystem
 		$this->db = $db;
 	}
 	
+	
+	/**
+	 * Load record in memory
+	 *
+	 * @param 	int 	$rowid 				   Id
+	 * @param 	string 	$ref             	   ref
+	 * @return 	int                            <0 if KO, Id of record if OK and found
+	 */
+	function fetch($rowid = 0, $ref = '') 
+	{
+	    global $conf;
+	
+	    if ($rowid > 0 || $ref) 
+	    {
+	        $sql  = "SELECT a.pcg_version, a.label, a.active";
+	        $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_system as a";
+	        $sql .= " WHERE";
+	        if ($rowid) {
+	            $sql .= " a.rowid = '" . $rowid . "'";
+	        } elseif ($ref) {
+	            $sql .= " a.pcg_version = '" . $ref . "'";
+	        }
+	
+	        dol_syslog(get_class($this) . "::fetch sql=" . $sql, LOG_DEBUG);
+	        $result = $this->db->query($sql);
+	        if ($result) {
+	            $obj = $this->db->fetch_object($result);
+	
+	            if ($obj) {
+	                $this->id = $obj->rowid;
+	                $this->rowid = $obj->rowid;
+	                $this->pcg_version = $obj->pcg_version;
+	                $this->ref = $obj->pcg_version;
+	                $this->label = $obj->label;
+	                $this->active = $obj->active;
+	                	
+	                return $this->id;
+	            } else {
+	                return 0;
+	            }
+	        } else {
+	            $this->error = "Error " . $this->db->lasterror();
+	            $this->errors[] = "Error " . $this->db->lasterror();
+	        }
+	    }
+	    return - 1;
+	}
+	
+	
 	/**
 	 * Insert accountancy system name into database
 	 *

+ 25 - 10
htdocs/accountancy/class/accountingaccount.class.php

@@ -150,12 +150,19 @@ class AccountingAccount extends CommonObject
 		if (isset($this->active))
 			$this->active = trim($this->active);
 			
-			// Check parameters
-			// Put here code to add control on parameters values
+		if (empty($this->pcg_type) || $this->pcg_type == '-1')
+		{
+		    $this->pcg_type = 'XXXXXX';
+		}
+		if (empty($this->pcg_subtype) || $this->pcg_subtype == '-1')
+		{
+		    $this->pcg_subtype = 'XXXXXX';
+		}
+		// Check parameters
+		// Put here code to add control on parameters values
 			
 		// Insert request
 		$sql = "INSERT INTO " . MAIN_DB_PREFIX . "accounting_account(";
-		
 		$sql .= "datec";
 		$sql .= ", entity";
 		$sql .= ", fk_pcg_version";
@@ -167,9 +174,7 @@ class AccountingAccount extends CommonObject
 		$sql .= ", fk_accounting_category";
 		$sql .= ", fk_user_author";
 		$sql .= ", active";
-		
 		$sql .= ") VALUES (";
-		
 		$sql .= " '" . $this->db->idate($now) . "'";
 		$sql .= ", " . $conf->entity;
 		$sql .= ", " . (empty($this->fk_pcg_version) ? 'NULL' : "'" . $this->db->escape($this->fk_pcg_version) . "'");
@@ -225,11 +230,22 @@ class AccountingAccount extends CommonObject
 	/**
 	 * Update record
 	 *
-	 * @param User $user Use making update
-	 * @return int <0 if KO, >0 if OK
+	 * @param  User $user      Use making update
+	 * @return int             <0 if KO, >0 if OK
 	 */
-	function update($user) {
-		$this->db->begin();
+	function update($user) 
+	{
+	    // Check parameters
+	    if (empty($this->pcg_type) || $this->pcg_type == '-1')
+	    {
+	        $this->pcg_type = 'XXXXXX';
+	    }
+	    if (empty($this->pcg_subtype) || $this->pcg_subtype == '-1')
+	    {
+	        $this->pcg_subtype = 'XXXXXX';
+	    }
+	     
+	    $this->db->begin();
 		
 		$sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account ";
 		$sql .= " SET fk_pcg_version = " . ($this->fk_pcg_version ? "'" . $this->db->escape($this->fk_pcg_version) . "'" : "null");
@@ -241,7 +257,6 @@ class AccountingAccount extends CommonObject
 		$sql .= " , fk_accounting_category = '" . $this->account_category . "'";
 		$sql .= " , fk_user_modif = " . $user->id;
 		$sql .= " , active = '" . $this->active . "'";
-		
 		$sql .= " WHERE rowid = " . $this->id;
 		
 		dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);

+ 16 - 7
htdocs/accountancy/class/html.formventilation.class.php

@@ -180,11 +180,15 @@ class FormVentilation extends Form
 		$options = array();
 		$out = ajax_combobox($htmlname, $event);
 
-		while ($obj = $this->db->fetch_object($resql)) {
-			$options[$obj->pcg_type] = $obj->pcg_type;
+		while ($obj = $this->db->fetch_object($resql)) 
+		{
+		    if ($obj->pcg_type != '-1')
+		    {
+                $options[$obj->pcg_type] = $obj->pcg_type;
+		    }
 		}
 
-		$out .= Form::selectarray($htmlname, $options, $selectid, $showempty);
+		$out .= Form::selectarray($htmlname, $options, $selectid, $showempty, 0, 0, '', 0, 0, 0, '', 'minwidth200');
 
 		$this->db->free($resql);
 		return $out;
@@ -200,7 +204,8 @@ class FormVentilation extends Form
 	 *
 	 * @return string String with HTML select
 	 */
-	function select_pcgsubtype($selectid, $htmlname = 'pcg_subtype', $showempty = 0, $event = array()) {
+	function select_pcgsubtype($selectid, $htmlname = 'pcg_subtype', $showempty = 0, $event = array()) 
+	{
 		global $conf;
 
 		$sql = "SELECT DISTINCT pcg_subtype ";
@@ -221,11 +226,15 @@ class FormVentilation extends Form
 		$options = array();
 		$out = ajax_combobox($htmlname, $event);
 
-		while ($obj = $this->db->fetch_object($resql)) {
-			$options[$obj->pcg_subtype] = $obj->pcg_subtype;
+		while ($obj = $this->db->fetch_object($resql)) 
+		{
+		    if ($obj->pcg_type != '-1')
+		    {
+                $options[$obj->pcg_subtype] = $obj->pcg_subtype;
+		    }
 		}
 
-		$out .= Form::selectarray($htmlname, $options, $selectid, $showempty);
+		$out .= Form::selectarray($htmlname, $options, $selectid, $showempty, 0, 0, '', 0, 0, 0, '', 'minwidth200');
 
 		$this->db->free($resql);
 		return $out;

+ 13 - 9
htdocs/core/class/html.formaccounting.class.php

@@ -90,31 +90,35 @@ class FormAccounting
             $num = $db->num_rows($resql);
             if ($num)
             {
-                print '<select class="flat minwidth200" name="'.$htmlname.'">';
+                $out = '<select class="flat minwidth200" id="'.$htmlname.'" name="'.$htmlname.'">';
                 $i = 0;
 
-                if ($useempty) print '<option value="0">&nbsp;</option>';
+                if ($useempty) $out.= '<option value="0">&nbsp;</option>';
                 while ($i < $num)
                 {
                     $obj = $db->fetch_object($resql);
-                    print '<option value="'.$obj->rowid.'"';
-                    if ($obj->rowid == $selected) print ' selected';
-                    print '>'.($maxlen ? dol_trunc($obj->type,$maxlen) : $obj->type);
-					print ' ('.$obj->range_account.')';
+                    $out .= '<option value="'.$obj->rowid.'"';
+                    if ($obj->rowid == $selected) $out .= ' selected';
+                    $out .= '>'.($maxlen ? dol_trunc($obj->type,$maxlen) : $obj->type);
+					$out .= ' ('.$obj->range_account.')';
                     $i++;
                 }
-                print '</select>';
-                if ($user->admin && $help) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
+                $out .=  '</select>';
+                if ($user->admin && $help) $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
             }
             else
             {
-                print $langs->trans("ErrorNoAccountingCategoryForThisCountry",$mysoc->country_code);
+                $out .= $langs->trans("ErrorNoAccountingCategoryForThisCountry",$mysoc->country_code);
             }
         }
         else
         {
             dol_print_error($db,$db->lasterror());
         }
+        
+        $out .= ajax_combobox($htmlname, $event);
+        
+        print $out;
     }
 }
 

+ 2 - 2
htdocs/langs/en_US/compta.lang

@@ -13,8 +13,8 @@ LTReportBuildWithOptionDefinedInModule=Amounts shown here are calculated using r
 Param=Setup
 RemainingAmountPayment=Amount payment remaining :
 Account=Account
-Accountparent=Account parent
-Accountsparent=Accounts parent
+Accountparent=Parent account
+Accountsparent=Parent accounts
 Income=Income
 Outcome=Expense
 ReportInOut=Income / Expense