79function pt($db, $sql, $date)
 
   81  global $conf, $bc, $langs, $form;
 
   83  $result = $db->query($sql);
 
   85    $num = $db->num_rows($result);
 
   88    print 
'<table class="noborder centpercent">';
 
   90    print 
'<tr class="liste_titre">';
 
   91    print 
'<td class="nowrap">'.$date.
'</td>';
 
   92    print 
'<td class="right">'.$langs->trans(
"ClaimedForThisPeriod").
'</td>';
 
   93    print 
'<td class="right">'.$langs->trans(
"PaidDuringThisPeriod").$form->textwithpicto(
'', $langs->trans(
'PaidDuringThisPeriodDesc'), 1).
'</td>';
 
  105      $obj = $db->fetch_object($result);
 
  109      if ($obj->mode == 
'claimed' && !empty($previousmode)) {
 
  110        print 
'<tr class="oddeven">';
 
  111        print 
'<td class="nowrap">'.$previousmonth.
"</td>\n";
 
  112        print 
'<td class="nowrap right"><span class="amount">'.price($amountclaimed).
"</span></td>\n";
 
  113        print 
'<td class="nowrap right"><span class="amount">'.price($amountpaid).
"</span></td>\n";
 
  120      if ($obj->mode == 
'claimed') {
 
  121        $amountclaimed = $obj->mm;
 
  122        $totalclaimed = $totalclaimed + $amountclaimed;
 
  124      if ($obj->mode == 
'paid') {
 
  125        $amountpaid = $obj->mm;
 
  126        $totalpaid = $totalpaid + $amountpaid;
 
  129      if ($obj->mode == 
'paid') {
 
  130        print 
'<tr class="oddeven">';
 
  131        print 
'<td class="nowrap">'.$obj->dm.
"</td>\n";
 
  132        print 
'<td class="nowrap right"><span class="amount">'.price($amountclaimed).
"</span></td>\n";
 
  133        print 
'<td class="nowrap right"><span class="amount">'.price($amountpaid).
"</span></td>\n";
 
  140        $previousmode = $obj->mode;
 
  141        $previousmonth = $obj->dm;
 
  147    if ($mode == 
'claimed' && !empty($previousmode)) {
 
  148      print 
'<tr class="oddeven">';
 
  149      print 
'<td class="nowrap">'.$previousmonth.
"</td>\n";
 
  150      print 
'<td class="nowrap right">'.price($amountclaimed).
"</td>\n";
 
  151      print 
'<td class="nowrap right">'.price($amountpaid).
"</td>\n";
 
  158    print 
'<tr class="liste_total">';
 
  159    print 
'<td class="right">'.$langs->trans(
"Total").
'</td>';
 
  160    print 
'<td class="nowrap right">'.price($totalclaimed).
'</td>';
 
  161    print 
'<td class="nowrap right">'.price($totalpaid).
'</td>';
 
  177$form = 
new Form($db);
 
  178$company_static = 
new Societe($db);
 
  181$fsearch = 
'<!-- hidden fields for form -->';
 
  182$fsearch .= 
'<input type="hidden" name="token" value="'.newToken().
'">';
 
  183$fsearch .= 
'<input type="hidden" name="modetax" value="'.$modetax.
'">';
 
  185$description = $fsearch;
 
  188$name = $langs->trans(
"VATReportByMonth");
 
  191  $calcmode = $langs->trans(
'OptionVATDefault');
 
  194  $calcmode = $langs->trans(
'OptionVATDebitOption');
 
  197  $calcmode = $langs->trans(
'OptionPaymentForProductAndServices');
 
  199$calcmode .= 
' <span class="opacitymedium">('.$langs->trans(
"TaxModuleSetupToModifyRules", DOL_URL_ROOT.
'/admin/taxes.php').
')</span>';
 
  201$description .= $langs->trans(
"VATSummary").
'<br>';
 
  203  $description .= $langs->trans(
"RulesVATDueProducts");
 
  206  $description .= $langs->trans(
"RulesVATInProducts");
 
  209  $description .= 
'<br>'.$langs->trans(
"RulesVATDueServices");
 
  212  $description .= 
'<br>'.$langs->trans(
"RulesVATInServices");
 
  215  $description .= 
'<br>'.$langs->trans(
"DepositsAreNotIncluded");
 
  218  $description .= $langs->trans(
"SupplierDepositsAreNotIncluded");
 
  220if (isModEnabled(
'accounting')) {
 
  221  $description .= 
'<br>'.$langs->trans(
"ThisIsAnEstimatedValue");
 
  224$period = $form->selectDate($date_start, 
'date_start', 0, 0, 0, 
'', 1, 0).
' - '.$form->selectDate($date_end, 
'date_end', 0, 0, 0, 
'', 1, 0);
 
  238report_header($name, 
'', $period, $periodlink, $description, $builddate, $exportlink, array(), $calcmode);
 
  244if ($refresh === 
true) {
 
  245  print 
'<div class="fichecenter"><div class="fichethirdleft">';
 
  249  print 
'<table class="noborder centpercent">';
 
  250  print 
'<tr class="liste_titre">';
 
  251  print 
'<td width="30%">' . $langs->trans(
"Year") . 
'</td>';
 
  252  print 
'<td class="right">' . $langs->trans(
"VATToPay") . 
'</td>';
 
  253  print 
'<td class="right">' . $langs->trans(
"VATToCollect") . 
'</td>';
 
  254  print 
'<td class="right">' . $langs->trans(
"Balance") . 
'</td>';
 
  255  print 
'<td> </td>' . 
"\n";
 
  256  print 
'</tr>' . 
"\n";
 
  262  $yend = $tmp[
'year'];
 
  272  while ((($y < $yend) || ($y == $yend && $m <= $mend)) && $mcursor < 1000) {    
 
  282    $x_coll = 
tax_by_rate(
'vat', $db, $y, 0, 0, 0, $modetax, 
'sell', $m);
 
  283    $x_paye = 
tax_by_rate(
'vat', $db, $y, 0, 0, 0, $modetax, 
'buy', $m);
 
  287    foreach (array_keys($x_coll) as $my_coll_rate) {
 
  288      $x_both[$my_coll_rate][
'coll'][
'totalht'] = $x_coll[$my_coll_rate][
'totalht'];
 
  289      $x_both[$my_coll_rate][
'coll'][
'vat'] = $x_coll[$my_coll_rate][
'vat'];
 
  290      $x_both[$my_coll_rate][
'paye'][
'totalht'] = 0;
 
  291      $x_both[$my_coll_rate][
'paye'][
'vat'] = 0;
 
  292      $x_both[$my_coll_rate][
'coll'][
'links'] = 
'';
 
  293      $x_both[$my_coll_rate][
'coll'][
'detail'] = array();
 
  294      foreach ($x_coll[$my_coll_rate][
'facid'] as $id => $dummy) {
 
  299        $x_both[$my_coll_rate][
'coll'][
'detail'][] = array(
 
  300          'id' => $x_coll[$my_coll_rate][
'facid'][$id],
 
  301          'descr' => $x_coll[$my_coll_rate][
'descr'][$id],
 
  302          'pid' => $x_coll[$my_coll_rate][
'pid'][$id],
 
  303          'pref' => $x_coll[$my_coll_rate][
'pref'][$id],
 
  304          'ptype' => $x_coll[$my_coll_rate][
'ptype'][$id],
 
  305          'payment_id' => $x_coll[$my_coll_rate][
'payment_id'][$id],
 
  306          'payment_amount' => $x_coll[$my_coll_rate][
'payment_amount'][$id],
 
  307          'ftotal_ttc' => $x_coll[$my_coll_rate][
'ftotal_ttc'][$id],
 
  308          'dtotal_ttc' => $x_coll[$my_coll_rate][
'dtotal_ttc'][$id],
 
  309          'dtype' => $x_coll[$my_coll_rate][
'dtype'][$id],
 
  310          'datef' => $x_coll[$my_coll_rate][
'datef'][$id],
 
  311          'datep' => $x_coll[$my_coll_rate][
'datep'][$id],
 
  313          'ddate_start' => $x_coll[$my_coll_rate][
'ddate_start'][$id],
 
  314          'ddate_end' => $x_coll[$my_coll_rate][
'ddate_end'][$id],
 
  315          'totalht' => $x_coll[$my_coll_rate][
'totalht_list'][$id],
 
  316          'vat' => $x_coll[$my_coll_rate][
'vat_list'][$id],
 
  323    foreach (array_keys($x_paye) as $my_paye_rate) {
 
  324      $x_both[$my_paye_rate][
'paye'][
'totalht'] = $x_paye[$my_paye_rate][
'totalht'];
 
  325      $x_both[$my_paye_rate][
'paye'][
'vat'] = $x_paye[$my_paye_rate][
'vat'];
 
  326      if (!isset($x_both[$my_paye_rate][
'coll'][
'totalht'])) {
 
  327        $x_both[$my_paye_rate][
'coll'][
'totalht'] = 0;
 
  328        $x_both[$my_paye_rate][
'coll'][
'vat'] = 0;
 
  330      $x_both[$my_paye_rate][
'paye'][
'links'] = 
'';
 
  331      $x_both[$my_paye_rate][
'paye'][
'detail'] = array();
 
  333      foreach ($x_paye[$my_paye_rate][
'facid'] as $id => $dummy) {
 
  335        if ($x_paye[$my_paye_rate][
'ptype'][$id] == 
'ExpenseReportPayment') {
 
  340          $x_both[$my_paye_rate][
'paye'][
'detail'][] = array(
 
  341            'id' => $x_paye[$my_paye_rate][
'facid'][$id],
 
  342            'descr' => $x_paye[$my_paye_rate][
'descr'][$id],
 
  343            'pid' => $x_paye[$my_paye_rate][
'pid'][$id],
 
  344            'pref' => $x_paye[$my_paye_rate][
'pref'][$id],
 
  345            'ptype' => $x_paye[$my_paye_rate][
'ptype'][$id],
 
  346            'payment_id' => $x_paye[$my_paye_rate][
'payment_id'][$id],
 
  347            'payment_amount' => $x_paye[$my_paye_rate][
'payment_amount'][$id],
 
  348            'ftotal_ttc' => 
price2num($x_paye[$my_paye_rate][
'ftotal_ttc'][$id]),
 
  349            'dtotal_ttc' => 
price2num($x_paye[$my_paye_rate][
'dtotal_ttc'][$id]),
 
  350            'dtype' => $x_paye[$my_paye_rate][
'dtype'][$id],
 
  351            'ddate_start' => $x_paye[$my_paye_rate][
'ddate_start'][$id],
 
  352            'ddate_end' => $x_paye[$my_paye_rate][
'ddate_end'][$id],
 
  353            'totalht' => 
price2num($x_paye[$my_paye_rate][
'totalht_list'][$id]),
 
  354            'vat' => $x_paye[$my_paye_rate][
'vat_list'][$id],
 
  362          $x_both[$my_paye_rate][
'paye'][
'detail'][] = array(
 
  363            'id' => $x_paye[$my_paye_rate][
'facid'][$id],
 
  364            'descr' => $x_paye[$my_paye_rate][
'descr'][$id],
 
  365            'pid' => $x_paye[$my_paye_rate][
'pid'][$id],
 
  366            'pref' => $x_paye[$my_paye_rate][
'pref'][$id],
 
  367            'ptype' => $x_paye[$my_paye_rate][
'ptype'][$id],
 
  368            'payment_id' => $x_paye[$my_paye_rate][
'payment_id'][$id],
 
  369            'payment_amount' => $x_paye[$my_paye_rate][
'payment_amount'][$id],
 
  370            'ftotal_ttc' => 
price2num($x_paye[$my_paye_rate][
'ftotal_ttc'][$id]),
 
  371            'dtotal_ttc' => 
price2num($x_paye[$my_paye_rate][
'dtotal_ttc'][$id]),
 
  372            'dtype' => $x_paye[$my_paye_rate][
'dtype'][$id],
 
  373            'datef' => $x_paye[$my_paye_rate][
'datef'][$id],
 
  374            'datep' => $x_paye[$my_paye_rate][
'datep'][$id],
 
  376            'ddate_start' => $x_paye[$my_paye_rate][
'ddate_start'][$id],
 
  377            'ddate_end' => $x_paye[$my_paye_rate][
'ddate_end'][$id],
 
  378            'totalht' => 
price2num($x_paye[$my_paye_rate][
'totalht_list'][$id]),
 
  379            'vat' => $x_paye[$my_paye_rate][
'vat_list'][$id],
 
  388    $object = array(&$x_coll, &$x_paye, &$x_both);
 
  389    $parameters[
"mode"] = $modetax;
 
  390    $parameters[
"year"] = $y;
 
  391    $parameters[
"month"] = $m;
 
  392    $parameters[
"type"] = 
'vat';
 
  395    $hookmanager->initHooks(array(
'externalbalance'));
 
  396    $reshook = $hookmanager->executeHooks(
'addVatLine', $parameters, $object, $action); 
 
  399    print 
'<tr class="oddeven">';
 
  400    print 
'<td class="nowrap"><a href="' . DOL_URL_ROOT . 
'/compta/tva/quadri_detail.php?leftmenu=tax_vat&month=' . $m . 
'&year=' . $y . 
'">' . 
dol_print_date(
dol_mktime(0, 0, 0, $m, 1, $y), 
"%b %Y") . 
'</a></td>';
 
  403    foreach (array_keys($x_coll) as $rate) {
 
  404      $subtot_coll_total_ht = 0;
 
  405      $subtot_coll_vat = 0;
 
  407      foreach ($x_both[$rate][
'coll'][
'detail'] as $index => $fields) {
 
  409        $ratiopaymentinvoice = 1;
 
  413          $type = (isset($fields[
'dtype']) ? $fields[
'dtype'] : $fields[
'ptype']);
 
  416          if (!empty($fields[
'ddate_start'])) {
 
  419          if (!empty($fields[
'ddate_end'])) {
 
  427            if (isset($fields[
'payment_amount']) && 
price2num($fields[
'ftotal_ttc'])) {
 
  428              $ratiopaymentinvoice = ($fields[
'payment_amount'] / $fields[
'ftotal_ttc']);
 
  433        $temp_ht = $fields[
'totalht'] * $ratiopaymentinvoice;
 
  434        $temp_vat = $fields[
'vat'] * $ratiopaymentinvoice;
 
  435        $subtot_coll_total_ht += $temp_ht;
 
  436        $subtot_coll_vat += $temp_vat;
 
  437        $x_coll_sum += $temp_vat;
 
  440    print 
'<td class="nowrap right"><span class="amount">' . 
price(
price2num($x_coll_sum, 
'MT')) . 
'</span></td>';
 
  443    foreach (array_keys($x_paye) as $rate) {
 
  444      $subtot_paye_total_ht = 0;
 
  445      $subtot_paye_vat = 0;
 
  447      foreach ($x_both[$rate][
'paye'][
'detail'] as $index => $fields) {
 
  449        $ratiopaymentinvoice = 1;
 
  453          $type = (isset($fields[
'dtype']) ? $fields[
'dtype'] : $fields[
'ptype']);
 
  456          if (!empty($fields[
'ddate_start'])) {
 
  459          if (!empty($fields[
'ddate_end'])) {
 
  467            if (isset($fields[
'payment_amount']) && 
price2num($fields[
'ftotal_ttc'])) {
 
  468              $ratiopaymentinvoice = ($fields[
'payment_amount'] / (float) $fields[
'ftotal_ttc']);
 
  473        $temp_ht = (float) $fields[
'totalht'] * $ratiopaymentinvoice;
 
  474        $temp_vat = $fields[
'vat'] * $ratiopaymentinvoice;
 
  475        $subtot_paye_total_ht += $temp_ht;
 
  476        $subtot_paye_vat += $temp_vat;
 
  477        $x_paye_sum += $temp_vat;
 
  480    print 
'<td class="nowrap right"><span class="amount">' . 
price(
price2num($x_paye_sum, 
'MT')) . 
'</span></td>';
 
  482    $subtotalcoll = $subtotalcoll + $x_coll_sum;
 
  483    $subtotalpaid = $subtotalpaid + $x_paye_sum;
 
  485    $diff = $x_coll_sum - $x_paye_sum;
 
  486    $total = $total + $diff;
 
  487    $subtotal = 
price2num($subtotal + $diff, 
'MT');
 
  489    print 
'<td class="nowrap right"><span class="amount">' . 
price(
price2num($diff, 
'MT')) . 
'</span></td>' . 
"\n";
 
  490    print 
"<td> </td>\n";
 
  497      print 
'<tr class="liste_total">';
 
  498      print 
'<td class="right"><a href="quadri_detail.php?leftmenu=tax_vat&q=' . round($m / 3) . 
'&year=' . $y . 
'">' . $langs->trans(
"SubTotal") . 
'</a>:</td>';
 
  499      print 
'<td class="nowrap right">' . 
price(
price2num($subtotalcoll, 
'MT')) . 
'</td>';
 
  500      print 
'<td class="nowrap right">' . 
price(
price2num($subtotalpaid, 
'MT')) . 
'</td>';
 
  501      print 
'<td class="nowrap right">' . 
price(
price2num($subtotal, 
'MT')) . 
'</td>';
 
  502      print 
'<td> </td></tr>';
 
  509  print 
'<tr class="liste_total"><td class="right" colspan="3">' . $langs->trans(
"TotalToPay") . 
':</td><td class="nowrap right">' . 
price(
price2num($total, 
'MT')) . 
'</td>';
 
  510  print 
"<td> </td>\n";
 
  516  print 
'</div><div class="fichetwothirdright">';
 
  526  $sql .= 
"SELECT SUM(amount) as mm, date_format(tva.datev,'%Y-%m') as dm, 'claimed' as mode";
 
  527  $sql .= 
" FROM " . MAIN_DB_PREFIX . 
"tva as tva";
 
  528  $sql .= 
" WHERE tva.entity = " . $conf->entity;
 
  529  $sql .= 
" AND (tva.datev >= '" . $db->idate($date_start) . 
"' AND tva.datev <= '" . $db->idate($date_end) . 
"')";
 
  530  $sql .= 
" GROUP BY dm";
 
  534  $sql .= 
"SELECT SUM(ptva.amount) as mm, date_format(tva.datev,'%Y-%m') as dm, 'paid' as mode";
 
  535  $sql .= 
" FROM " . MAIN_DB_PREFIX . 
"tva as tva";
 
  536  $sql .= 
" INNER JOIN " . MAIN_DB_PREFIX . 
"payment_vat as ptva ON (tva.rowid = ptva.fk_tva)";
 
  537  $sql .= 
" WHERE tva.entity = " . $conf->entity;
 
  538  $sql .= 
" AND (tva.datev >= '" . $db->idate($date_start) . 
"' AND tva.datev <= '" . $db->idate($date_end) . 
"')";
 
  539  $sql .= 
" GROUP BY dm";
 
  541  $sql .= 
" ORDER BY dm ASC, mode ASC";
 
  544  pt($db, $sql, $langs->trans(
"Month"));