Recurring invoices result in no dimensions on gl_trans. Fixed by company flag option.
[fa-stable.git] / sales / create_recurrent_invoices.php
index 8cdf543b606cc03033681997943b99a3dcfa7142..ea71fd3a903d421d8f51a7739ae6e1c3555afa09 100644 (file)
@@ -1,13 +1,13 @@
 <?php
 /**********************************************************************
-    Copyright (C) FrontAccounting, LLC.
+       Copyright (C) FrontAccounting, LLC.
        Released under the terms of the GNU General Public License, GPL, 
        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 License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+       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 License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
 $page_security = 'SA_SALESINVOICE';
 $path_to_root = "..";
@@ -18,63 +18,122 @@ include_once($path_to_root . "/includes/ui.inc");
 include_once($path_to_root . "/reporting/includes/reporting.inc");
 
 $js = "";
-if ($use_popup_windows)
+if ($SysPrefs->use_popup_windows)
        $js .= get_js_open_window(900, 600);
+if (user_use_date_picker())
+       $js .= get_js_date_picker();
 
-page(_("Create and Print Recurrent Invoices"), false, false, "", $js);
+page(_($help_context = "Create and Print Recurrent Invoices"), false, false, "", $js);
 
-function set_last_sent($id, $date)
+function create_recurrent_invoices($customer_id, $branch_id, $order_no, $tmpl_no, $date, $from, $to, $memo)
 {
-       $date = date2sql($date);
-       $sql = "UPDATE ".TB_PREF."recurrent_invoices SET last_sent='$date' WHERE id=$id";
-       db_query($sql,"The recurrent invoice could not be updated or added");
-}      
-       
-function create_recurrent_invoices($customer_id, $branch_id, $order_no, $tmpl_no)
-{
-       $doc = new Cart(30, array($order_no));
+       global $Refs, $SysPrefs;
+
+       update_last_sent_recurrent_invoice($tmpl_no, $to);
 
+       $doc = new Cart(ST_SALESORDER, array($order_no));
+       
+       if (!empty($SysPrefs->prefs['dim_on_recurrent_invoice']))
+               $doc->trans_type = ST_SALESINVOICE;
+       
        get_customer_details_to_order($doc, $customer_id, $branch_id);
 
-       $doc->trans_type = 30;
+       $doc->trans_type = ST_SALESORDER;
        $doc->trans_no = 0;
-       $doc->document_date = Today(); // 2006-06-15. Added so Invoices and Deliveries get current day
+       $doc->document_date = $date;
+
+       $doc->due_date = get_invoice_duedate($doc->payment, $doc->document_date);
 
-       $doc->due_date = get_invoice_duedate($doc->customer_id, $doc->document_date);
-       $doc->reference = references::get_next($doc->trans_type);
-       //$doc->Comments='';
+       $doc->reference = $Refs->get_next($doc->trans_type, null, array('customer' => $customer_id, 'branch' => $branch_id,
+               'date' => $date));
+       $doc->Comments = $memo;
 
        foreach ($doc->line_items as $line_no=>$item) {
                $line = &$doc->line_items[$line_no];
-               $line->price = get_price($line->stock_id, $doc->customer_currency,
+               $new_price = get_price($line->stock_id, $doc->customer_currency,
                        $doc->sales_type, $doc->price_factor, $doc->document_date);
+               if ($new_price != 0)    // use template price if no price is currently set for the item.
+                       $line->price = $new_price;
        }       
        $cart = $doc;
-       $cart->trans_type = 10;
-       $cart->reference = references::get_next($cart->trans_type);
+       $cart->trans_type = ST_SALESINVOICE;
+       $cart->reference = $Refs->get_next($cart->trans_type);
+       $cart->payment_terms['cash_sale'] = false; // no way to register cash payment with recurrent invoice at once
        $invno = $cart->write(1);
-       set_last_sent($tmpl_no, $cart->document_date);
+
        return $invno;
 }
 
-if (isset($_GET['recurrent']))
+function calculate_from($myrow)
 {
-       $invs = array();
-       $sql = "SELECT * FROM ".TB_PREF."recurrent_invoices WHERE id=".$_GET['recurrent'];
+       if ($myrow["last_sent"] == '0000-00-00')
+               $from = sql2date($myrow["begin"]);
+       else
+               $from = sql2date($myrow["last_sent"]);
+       return $from;
+}
 
-       $result = db_query($sql,"could not get recurrent invoice");
-       $myrow = db_fetch($result);
-       if ($myrow['debtor_no'] == 0)
-       {
-               $cust = get_cust_branches_from_group($myrow['group_no']);
-               while ($row = db_fetch($cust))
-               {
-                       $invs[] = create_recurrent_invoices($row['debtor_no'], $row['branch_code'], $myrow['order_no'], $myrow['id']);
-               }       
-       }
+function calculate_next($myrow)
+{
+       if ($myrow["last_sent"] == '0000-00-00')
+               $next = sql2date($myrow["begin"]);
        else
+               $next = sql2date($myrow["last_sent"]);
+       $next = add_months($next, $myrow['monthly']);
+       $next = add_days($next, $myrow['days']);
+       return add_days($next,-1);
+}
+
+$id = find_submit("confirmed");
+if ($id != -1 && is_date_closed($_POST['trans_date']))
+{
+       display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
+       set_focus('trans_date');
+       $_POST['create'.$id] = 1;       //re-display current page
+       $id = -1;
+}
+
+if ($id != -1)
+{
+       /*
+               whole invoiced time is <begin, end>
+               invoices are issued _after_ invoiced period is gone, eg:
+               begin 1.1
+               end       31.3
+               period:    invoice ready for issue since:
+               1.1-31.1 -      1.2
+               1.2-28.2 -      1.3
+               1.3-31.3 -      1.4
+               In example above, when end is set to 1.4 will generate additional invoice on 1.5 !
+       */
+
+       $Ajax->activate('_page_body');
+       $from = get_post('from');
+       $to = get_post('to');
+       $memo = get_post('memo');
+       $date = $_POST['trans_date'];
+       $myrow = get_recurrent_invoice($id);
+
+       $invs = array();
+       if (recurrent_invoice_ready($id, $date))
        {
-               $invs[] = create_recurrent_invoices($myrow['debtor_no'], $myrow['group_no'], $myrow['order_no'], $myrow['id']);
+                       begin_transaction();
+
+                       if ($myrow['debtor_no'] == 0)
+                       {
+                               $cust = get_cust_branches_from_group($myrow['group_no']);
+                               while ($row = db_fetch($cust))
+                               {
+                                       $invs[] = create_recurrent_invoices($row['debtor_no'], $row['branch_code'], $myrow['order_no'], $myrow['id'],
+                                               $date, $from, $to, $memo);
+                               }
+                       }
+                       else
+                       {
+                               $invs[] = create_recurrent_invoices($myrow['debtor_no'], $myrow['group_no'], $myrow['order_no'], $myrow['id'],
+                                       $date, $from, $to, $memo);
+                       }
+                       commit_transaction();
        }
        if (count($invs) > 0)
        {
@@ -83,86 +142,121 @@ if (isset($_GET['recurrent']))
        }
        else 
                $min = $max = 0;
-       display_notification(sprintf(_("%s recurrent invoice(s) created, # $min - # $max."), count($invs)));
+       display_notification(sprintf(_("%s recurrent invoice(s) created, # %s - # %s."), count($invs), $min, $max));
        if (count($invs) > 0)
        {
-               $ar = array('PARAM_0' => $min,  'PARAM_1' => $max, 'PARAM_2' => "", 'PARAM_3' => get_first_bank_account(), 
-                       'PARAM_4' => 0, 'PARAM_5' => 0, 'PARAM_6' => "", 'PARAM_7' => 10);
-               display_note(print_link(_("&Print Recurrent Invoices # $min - # $max"), 107, $ar), 0, 1);
-               $ar['PARAM_4'] = 1; 
-               display_note(print_link(_("&Email Recurrent Invoices # $min - # $max"), 107, $ar), 0, 1);
+               $ar = array('PARAM_0' => $min."-".ST_SALESINVOICE,      'PARAM_1' => $max."-".ST_SALESINVOICE, 'PARAM_2' => "",
+                       'PARAM_3' => 0, 'PARAM_4' => 0, 'PARAM_5' => "", 'PARAM_6' => "", 'PARAM_7' => user_def_print_orientation());
+               display_note(print_link(sprintf(_("&Print Recurrent Invoices # %s - # %s"), $min, $max), 107, $ar), 0, 1);
+               $ar['PARAM_3'] = 1; // email
+               display_note(print_link(sprintf(_("&Email Recurrent Invoices # %s - # %s"), $min, $max), 107, $ar), 0, 1);
        }
-}      
+}
+
 
-//-------------------------------------------------------------------------------------------------
-function get_sales_group_name($group_no)
+$id = find_submit('create');
+if ($id != -1)
 {
-       $sql = "SELECT description FROM ".TB_PREF."groups WHERE id = $group_no";
-       $result = db_query($sql, "could not get group");
-       $row = db_fetch($result);
-       return $row[0];
-}
+       $Ajax->activate('_page_body');
+       $date = Today();
+       $myrow = get_recurrent_invoice($id);
+       $from = calculate_from($myrow);
+       $to = add_months($from, $myrow['monthly']);
+       $to = add_days($to, $myrow['days']);
 
-$sql = "SELECT * FROM ".TB_PREF."recurrent_invoices ORDER BY description, group_no, debtor_no";
-$result = db_query($sql,"could not get recurrent invoices");
+       if (!is_date_in_fiscalyear($date))
+               display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
+       elseif (!date1_greater_date2(add_days(Today(), 1), $to))
+               display_error(_("Recurrent invoice cannot be generated before last day of covered period."));
+       elseif (check_recurrent_invoice_prices($id))
+               display_error(_("Recurrent invoices cannot be generated because some items have no price defined in customer currency."));
+       elseif (!check_sales_order_type($myrow['order_no']))
+               display_error(_("Recurrent invoices cannot be generated because selected sales order template uses prepayment sales terms. Change payment terms and try again."));
+       else {
+               $count = recurrent_invoice_count($id);
 
-start_table("$table_style width=70%");
-$th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Last Created"),"");
+               $_POST['trans_date'] = $to;
+               start_form();
+               start_table(TABLESTYLE, "width=50%");
+               label_row(_('Description:'), $myrow["description"]);
+               label_row(_('Template:'), get_customer_trans_view_str(ST_SALESORDER, $myrow["order_no"]));
+               label_row(_('Number of invoices:'), $count);
+               date_row(_('Invoice date:'), 'trans_date');
+               $newto = add_months($to, $myrow['monthly']);
+               $newto = add_days($newto, $myrow['days']);
+               text_row(_('Invoice notice:'), 'memo', sprintf(_("Recurrent Invoice covers period %s - %s."), $to,       add_days($newto, -1)), 100, 100);
+               //text_row(_('Invoice notice:'), 'memo', sprintf(_("Recurrent Invoice covers period %s - %s."), //$from, add_days($to, -1)), 100, 100);
+               end_table();
+               hidden('from', $from, true);
+               hidden('to', $to, true);
+               br();
+               submit_center_first('confirmed'.$id, _('Create'), _('Create recurrent invoices'), false, ICON_OK);
+               submit_center_last('cancel', _('Cancel'), _('Return to recurrent invoices'), false, ICON_ESCAPE);
+               submit_js_confirm("do_create".$id, sprintf(_("You are about to issue %s invoices.\n Do you want to continue?"), $count));
+               end_form();
+
+               display_footer_exit();
+       }
+}
+else
+{
+$result = get_recurrent_invoices(Today());
+
+start_form();
+start_table(TABLESTYLE, "width=70%");
+$th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Next invoice"),"");
 table_header($th);
 $k = 0;
-$today = add_days(Today(), 1);
 $due = false;
 while ($myrow = db_fetch($result)) 
 {
-       $begin = sql2date($myrow["begin"]);
-       $end = sql2date($myrow["end"]);
-       $last_sent = sql2date($myrow["last_sent"]);
-       if ($myrow['monthly'] > 0)
-               $due_date = begin_month($last_sent);
-       else
-               $due_date = $last_sent;
-       $due_date = add_months($due_date, $myrow['monthly']);
-       $due_date = add_days($due_date, $myrow['days']);
-       $overdue = date1_greater_date2($today, $due_date) && date1_greater_date2($today, $begin)
-               && date1_greater_date2($end, $today);
-       if ($overdue)
+       if ($myrow['overdue'])
        {
                start_row("class='overduebg'");
                $due = true;
-       }       
-       else    
+       }
+       else
                alt_table_row_color($k);
-               
+
        label_cell($myrow["description"]);
-       label_cell(get_customer_trans_view_str(30, $myrow["order_no"]));
+       label_cell(get_customer_trans_view_str(ST_SALESORDER, $myrow["order_no"]), "nowrap align='right'");
        if ($myrow["debtor_no"] == 0)
        {
                label_cell("");
+
                label_cell(get_sales_group_name($myrow["group_no"]));
-       }       
+       }
        else
        {
                label_cell(get_customer_name($myrow["debtor_no"]));
                label_cell(get_branch_name($myrow['group_no']));
-       }       
+       }
        label_cell($myrow["days"]);
        label_cell($myrow['monthly']);
-       label_cell($begin);
-       label_cell($end);
-       label_cell($last_sent);
-       if ($overdue)
-               label_cell("<a href='$path_to_root/sales/create_recurrent_invoices.php?recurrent=" . $myrow["id"] . "'>" . _("Create Invoices") . "</a>");
-       else
-               label_cell("");
+       label_cell(sql2date($myrow['begin']),  "align='center'");
+       label_cell(sql2date($myrow['end']),      "align='center'");
+       label_cell(calculate_next($myrow),      "align='center'");
+       if ($myrow['overdue'])
+       {
+               $count = recurrent_invoice_count($myrow['id']);
+               if ($count)
+               {
+                       button_cell("create".$myrow["id"], sprintf(_("Create %s Invoice(s)"), $count), "", ICON_DOC, 'process');
+               } else {
+                       label_cell('');
+               }
+       }
+       else
+               label_cell("");
        end_row();
 }
 end_table();
+end_form();
 if ($due)
        display_note(_("Marked items are due."), 1, 0, "class='overduefg'");
 else
        display_note(_("No recurrent invoices are due."), 1, 0);
 
-echo '<br>';
-
+br();
+}
 end_page();
-?>