2016-02-09 20:01:43 +01:00
< ? php
/* Copyright ( C ) 2012 - 2013 Christophe Battarel < christophe . battarel @ altairis . fr >
* Copyright ( C ) 2014 Ferran Marcet < fmarcet @ 2 byte . es >
* Copyright ( C ) 2015 Marcos García < marcosgdf @ gmail . com >
* Copyright ( C ) 2016 Florian Henry < florian . henry @ open - concept . pro >
*
* 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 3 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 / margin / checkMargins . php
* \ingroup margin
* \brief Check margins
*/
require '../main.inc.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/margin/lib/margins.lib.php' ;
2018-05-26 21:11:25 +02:00
// Load translation files required by the page
2018-05-26 15:50:03 +02:00
$langs -> loadLangs ( array ( 'companies' , 'bills' , 'products' , 'margins' ));
2016-02-09 20:01:43 +01:00
2017-09-12 14:50:12 +02:00
$action = GETPOST ( 'action' , 'alpha' );
$massaction = GETPOST ( 'massaction' , 'alpha' );
$toselect = GETPOST ( 'toselect' , 'array' );
$contextpage = GETPOST ( 'contextpage' , 'aZ' ) ? GETPOST ( 'contextpage' , 'aZ' ) : 'margindetail' ; // To manage different context of search
2017-09-15 10:52:53 +02:00
$backtopage = GETPOST ( 'backtopage' , 'alpha' );
2017-09-12 14:50:12 +02:00
$optioncss = GETPOST ( 'optioncss' , 'alpha' );
2017-06-21 10:57:15 +02:00
// Load variable for pagination
$limit = GETPOST ( 'limit' , 'int' ) ? GETPOST ( 'limit' , 'int' ) : $conf -> liste_limit ;
$sortfield = GETPOST ( 'sortfield' , 'alpha' );
$sortorder = GETPOST ( 'sortorder' , 'alpha' );
$page = GETPOST ( 'page' , 'int' );
if ( empty ( $page ) || $page == - 1 ) { $page = 0 ; } // If $page is not defined, or '' or -1
$offset = $limit * $page ;
2016-02-09 20:01:43 +01:00
$pageprev = $page - 1 ;
$pagenext = $page + 1 ;
2017-06-21 10:57:15 +02:00
if ( ! $sortorder ) $sortorder = " DESC " ;
if ( ! $sortfield ) $sortfield = 'f.facnumber' ;
2016-02-09 20:01:43 +01:00
$startdate = $enddate = '' ;
$startdate = dol_mktime ( 0 , 0 , 0 , GETPOST ( 'startdatemonth' , 'int' ), GETPOST ( 'startdateday' , 'int' ), GETPOST ( 'startdateyear' , 'int' ));
$enddate = dol_mktime ( 23 , 59 , 59 , GETPOST ( 'enddatemonth' , 'int' ), GETPOST ( 'enddateday' , 'int' ), GETPOST ( 'enddateyear' , 'int' ));
2017-05-16 14:05:46 +02:00
$search_ref = GETPOST ( 'search_ref' , 'alpha' );
2017-06-21 10:57:15 +02:00
// Security check
$result = restrictedArea ( $user , 'margins' );
// Both test are required to be compatible with all browsers
if ( GETPOST ( " button_search_x " ) || GETPOST ( " button_search " )) {
$action = 'search' ;
} elseif ( GETPOST ( " button_updatemagins_x " ) || GETPOST ( " button_updatemagins " )) {
$action = 'update' ;
}
2017-05-16 14:05:46 +02:00
/*
* Actions
*/
2017-09-15 10:50:50 +02:00
if ( GETPOST ( 'cancel' , 'alpha' )) { $action = 'list' ; $massaction = '' ; }
if ( ! GETPOST ( 'confirmmassaction' , 'alpha' ) && $massaction != 'presend' && $massaction != 'confirm_presend' ) { $massaction = '' ; }
2017-05-16 14:05:46 +02:00
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'doActions' , $parameters , $object , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook < 0 ) setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
if ( empty ( $reshook ))
{
// Selection of new fields
include DOL_DOCUMENT_ROOT . '/core/actions_changeselectedfields.inc.php' ;
2017-06-21 10:57:15 +02:00
if ( $action == 'update' ) {
$datapost = $_POST ;
foreach ( $datapost as $key => $value ) {
if ( strpos ( $key , 'buyingprice_' ) !== false ) {
$tmp_array = explode ( '_' , $key );
if ( count ( $tmp_array ) > 0 ) {
$invoicedet_id = $tmp_array [ 1 ];
if ( ! empty ( $invoicedet_id )) {
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facturedet' ;
$sql .= ' SET buy_price_ht=\'' . price2num ( $value ) . '\'' ;
$sql .= ' WHERE rowid=' . $invoicedet_id ;
$result = $db -> query ( $sql );
if ( ! $result ) {
setEventMessages ( $db -> lasterror , null , 'errors' );
}
}
}
}
}
}
2017-05-16 14:05:46 +02:00
// Purge search criteria
2017-07-13 00:35:10 +02:00
if ( GETPOST ( 'button_removefilter_x' , 'alpha' ) || GETPOST ( 'button_removefilter.x' , 'alpha' ) || GETPOST ( 'button_removefilter' , 'alpha' )) // All tests are required to be compatible with all browsers
2017-05-16 14:05:46 +02:00
{
$search_ref = '' ;
$search_array_options = array ();
}
// Mass actions
/*
$objectclass = 'Product' ;
if (( string ) $type == '1' ) { $objectlabel = 'Services' ; }
if (( string ) $type == '0' ) { $objectlabel = 'Products' ; }
$permtoread = $user -> rights -> produit -> lire ;
$permtodelete = $user -> rights -> produit -> supprimer ;
$uploaddir = $conf -> product -> dir_output ;
include DOL_DOCUMENT_ROOT . '/core/actions_massactions.inc.php' ;
*/
}
2016-02-09 20:01:43 +01:00
/*
* View
*/
$userstatic = new User ( $db );
$companystatic = new Societe ( $db );
$invoicestatic = new Facture ( $db );
$productstatic = new Product ( $db );
$form = new Form ( $db );
2016-03-30 23:33:19 +02:00
$title = $langs -> trans ( " Margins " );
2016-02-09 20:01:43 +01:00
llxHeader ( '' , $title );
// print_fiche_titre($text);
2017-06-21 10:57:15 +02:00
$param = '' ;
if ( ! empty ( $contextpage ) && $contextpage != $_SERVER [ " PHP_SELF " ]) $param .= '&contextpage=' . $contextpage ;
if ( $limit > 0 && $limit != $conf -> liste_limit ) $param .= '&limit=' . $limit ;
2017-09-12 14:50:12 +02:00
if ( $search_ref != '' ) $param .= '&search_ref=' . urlencode ( $search_ref );
if ( ! empty ( $startdate )) $param .= '&startdatemonth=' . GETPOST ( 'startdatemonth' , 'int' ) . '&startdateday=' . GETPOST ( 'startdateday' , 'int' ) . '&startdateyear=' . GETPOST ( 'startdateyear' , 'int' );
if ( ! empty ( $enddate )) $param .= '&enddatemonth=' . GETPOST ( 'enddatemonth' , 'int' ) . '&enddateday=' . GETPOST ( 'enddateday' , 'int' ) . '&enddateyear=' . GETPOST ( 'enddateyear' , 'int' );
if ( $optioncss != '' ) $param .= '&optioncss=' . $optioncss ;
2017-06-21 10:57:15 +02:00
2016-02-09 20:01:43 +01:00
// Show tabs
$head = marges_prepare_head ( $user );
$picto = 'margin' ;
print '<form method="post" name="sel" action="' . $_SERVER [ 'PHP_SELF' ] . '">' ;
2016-09-28 21:08:25 +02:00
dol_fiche_head ( $head , $langs -> trans ( 'checkMargins' ), $title , 0 , $picto );
2016-02-09 20:01:43 +01:00
print '<table class="border" width="100%">' ;
2017-05-16 14:05:46 +02:00
print '<tr><td class="titlefield">' . $langs -> trans ( 'DateStart' ) . ' (' . $langs -> trans ( " DateValidation " ) . ')</td>' ;
2016-09-28 21:08:25 +02:00
print '<td>' ;
2016-03-25 15:53:44 +01:00
$form -> select_date ( $startdate , 'startdate' , '' , '' , 1 , " sel " , 1 , 1 );
2016-02-09 20:01:43 +01:00
print '</td>' ;
2016-09-28 21:08:25 +02:00
print '<td>' . $langs -> trans ( 'DateEnd' ) . ' (' . $langs -> trans ( " DateValidation " ) . ')</td>' ;
print '<td>' ;
2016-03-25 15:53:44 +01:00
$form -> select_date ( $enddate , 'enddate' , '' , '' , 1 , " sel " , 1 , 1 );
2016-02-09 20:01:43 +01:00
print '</td>' ;
print '<td style="text-align: center;">' ;
2016-03-30 23:33:19 +02:00
print '<input type="submit" class="button" value="' . dol_escape_htmltag ( $langs -> trans ( 'Refresh' )) . '" name="button_search" />' ;
2017-05-16 14:05:46 +02:00
print '</td>' ;
print '</tr>' ;
2016-02-09 20:01:43 +01:00
print " </table> " ;
2016-09-28 21:08:25 +02:00
dol_fiche_end ();
2017-05-16 14:05:46 +02:00
$arrayfields = array ();
$massactionbutton = '' ;
2016-02-09 20:01:43 +01:00
$sql = " SELECT " ;
$sql .= " f.facnumber, f.rowid as invoiceid, d.rowid as invoicedetid, d.buy_price_ht, d.total_ht, d.subprice, d.label, d.description , d.qty " ;
$sql .= " ,d.fk_product " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " facture as f " ;
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . " facturedet as d ON d.fk_facture = f.rowid " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product as p ON d.fk_product = p.rowid " ;
$sql .= " WHERE f.fk_statut > 0 " ;
2018-01-23 09:49:21 +01:00
$sql .= " AND f.entity IN ( " . getEntity ( 'facture' ) . " ) " ;
2017-05-16 14:05:46 +02:00
if ( ! empty ( $startdate )) $sql .= " AND f.datef >= ' " . $db -> idate ( $startdate ) . " ' " ;
if ( ! empty ( $enddate )) $sql .= " AND f.datef <= ' " . $db -> idate ( $enddate ) . " ' " ;
if ( $search_ref ) $sql .= natural_search ( 'f.facnumber' , $search_ref );
2016-02-09 20:01:43 +01:00
$sql .= " AND d.buy_price_ht IS NOT NULL " ;
$sql .= $db -> order ( $sortfield , $sortorder );
2017-01-15 20:49:20 +01:00
$nbtotalofrecords = '' ;
2016-02-09 20:01:43 +01:00
if ( empty ( $conf -> global -> MAIN_DISABLE_FULL_SCANLIST )) {
2017-06-21 10:57:15 +02:00
2016-02-09 20:01:43 +01:00
dol_syslog ( __FILE__ , LOG_DEBUG );
$result = $db -> query ( $sql );
2018-04-24 11:37:57 +02:00
$nbtotalofrecords = $db -> num_rows ( $result );
if (( $page * $limit ) > $nbtotalofrecords ) // if total resultset is smaller then paging size (filtering), goto and load page 0
{
$page = 0 ;
$offset = 0 ;
2016-02-09 20:01:43 +01:00
}
}
2017-06-21 10:57:15 +02:00
$sql .= $db -> plimit ( $limit + 1 , $offset );
2016-02-09 20:01:43 +01:00
$result = $db -> query ( $sql );
if ( $result ) {
$num = $db -> num_rows ( $result );
2017-06-21 10:57:15 +02:00
2016-02-09 20:01:43 +01:00
print '<br>' ;
2017-06-21 10:57:15 +02:00
print_barre_liste ( $langs -> trans ( " MarginDetails " ), $page , $_SERVER [ " PHP_SELF " ], $param , $sortfield , $sortorder , '' , $num , $nbtotalofrecords , '' , 0 , '' , '' , $limit );
2017-01-28 13:13:57 +01:00
if ( $conf -> global -> MARGIN_TYPE == " 1 " )
2017-08-02 13:31:53 +02:00
$labelcostprice = 'BuyingPrice' ;
2017-01-28 13:13:57 +01:00
else // value is 'costprice' or 'pmp'
2017-08-02 13:31:53 +02:00
$labelcostprice = 'CostPrice' ;
2017-06-21 10:57:15 +02:00
2016-11-27 13:49:46 +01:00
$moreforfilter = '' ;
2017-06-21 10:57:15 +02:00
2017-05-16 14:05:46 +02:00
$varpage = empty ( $contextpage ) ? $_SERVER [ " PHP_SELF " ] : $contextpage ;
//$selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
//if ($massactionbutton) $selectedfields.=$form->showCheckAddButtons('checkforselect', 1);
$selectedfields = '' ;
2017-06-21 10:57:15 +02:00
2016-11-27 13:49:46 +01:00
print '<div class="div-table-responsive">' ;
print '<table class="tagtable liste' . ( $moreforfilter ? " listwithfilterbefore " : " " ) . '">' . " \n " ;
2017-06-21 10:57:15 +02:00
2017-05-16 14:05:46 +02:00
print '<tr class="liste_titre liste_titre_search">' ;
print '<td><input type="text" name="search_ref" value="' . dol_escape_htmltag ( $search_ref ) . '"></td>' ;
print '<td></td>' ;
print '<td></td>' ;
print '<td></td>' ;
print '<td></td>' ;
print '<td></td>' ;
print '<td class="liste_titre" align="middle">' ;
$searchpitco = $form -> showFilterButtons ();
print $searchpitco ;
print '</td>' ;
print " </tr> \n " ;
2017-06-21 10:57:15 +02:00
2016-02-09 20:01:43 +01:00
print '<tr class="liste_titre">' ;
2017-08-02 13:31:53 +02:00
print_liste_field_titre ( " Ref " , $_SERVER [ " PHP_SELF " ], " f.facnumber " , " " , $param , '' , $sortfield , $sortorder );
print_liste_field_titre ( " Description " , $_SERVER [ " PHP_SELF " ], " " , " " , $param , 'width=20%' , $sortfield , $sortorder );
print_liste_field_titre ( " UnitPriceHT " , $_SERVER [ " PHP_SELF " ], " d.subprice " , " " , $param , 'align="right"' , $sortfield , $sortorder );
2017-06-21 10:57:15 +02:00
print_liste_field_titre ( $labelcostprice , $_SERVER [ " PHP_SELF " ], " d.buy_price_ht " , " " , $param , 'align="right"' , $sortfield , $sortorder );
2017-08-02 13:31:53 +02:00
print_liste_field_titre ( " Qty " , $_SERVER [ " PHP_SELF " ], " d.qty " , " " , $param , 'align="right"' , $sortfield , $sortorder );
print_liste_field_titre ( " AmountTTC " , $_SERVER [ " PHP_SELF " ], " d.total_ht " , " " , $param , 'align="right"' , $sortfield , $sortorder );
2017-06-21 10:57:15 +02:00
print_liste_field_titre ( $selectedfields , $_SERVER [ " PHP_SELF " ], " " , '' , $param , 'align="center"' , $sortfield , $sortorder , 'maxwidthsearch ' );
2016-02-09 20:01:43 +01:00
print " </tr> \n " ;
2017-06-21 10:57:15 +02:00
$i = 0 ;
while ( $i < min ( $num , $limit ))
{
$objp = $db -> fetch_object ( $result );
print '<tr class="oddeven">' ;
print '<td>' ;
$result_inner = $invoicestatic -> fetch ( $objp -> invoiceid );
if ( $result_inner < 0 ) {
setEventMessages ( $invoicestatic -> error , null , 'errors' );
} else {
print $invoicestatic -> getNomUrl ( 1 );
}
print '</td>' ;
print '<td>' ;
if ( ! empty ( $objp -> fk_product )) {
$result_inner = $productstatic -> fetch ( $objp -> fk_product );
2016-02-09 20:01:43 +01:00
if ( $result_inner < 0 ) {
2017-06-21 10:57:15 +02:00
setEventMessages ( $productstatic -> error , null , 'errors' );
2016-02-09 20:01:43 +01:00
} else {
2017-06-21 10:57:15 +02:00
print $productstatic -> getNomUrl ( 1 );
2016-02-09 20:01:43 +01:00
}
2017-06-21 10:57:15 +02:00
} else {
print $objp -> label ;
print ' ' ;
print $objp -> description ;
2016-02-09 20:01:43 +01:00
}
2017-06-21 10:57:15 +02:00
print '</td>' ;
print '<td align="right">' ;
print price ( $objp -> subprice );
print '</td>' ;
print '<td align="right">' ;
print '<input type="text" name="buyingprice_' . $objp -> invoicedetid . '" id="buyingprice_' . $objp -> invoicedetid . '" size="6" value="' . price ( $objp -> buy_price_ht ) . '" class="right flat">' ;
print '</td>' ;
print '<td align="right">' ;
print $objp -> qty ;
print '</td>' ;
print '<td align="right">' ;
print price ( $objp -> total_ht );
print '</td>' ;
print '<td></td>' ;
print " </tr> \n " ;
$i ++ ;
2016-02-09 20:01:43 +01:00
}
2017-06-21 10:57:15 +02:00
2016-02-09 20:01:43 +01:00
print " </table> " ;
2017-06-21 10:57:15 +02:00
2016-11-27 13:49:46 +01:00
print " </div> " ;
2016-02-09 20:01:43 +01:00
} else {
dol_print_error ( $db );
}
2016-03-30 23:33:19 +02:00
2017-05-16 14:05:46 +02:00
print '<div class="center">' . " \n " ;
print '<input type="submit" class="button" name="button_updatemagins" id="button_updatemagins" value="' . $langs -> trans ( " Update " ) . '">' ;
2016-02-09 20:01:43 +01:00
print '</div>' ;
2016-09-28 21:08:25 +02:00
2016-02-09 20:01:43 +01:00
print '</form>' ;
$db -> free ( $result );
llxFooter ();
2018-05-26 15:50:03 +02:00
$db -> close ();