$versions{'Order_Manager-ext_lib.pl'} = '06.6.00.0000';

# Contains all sub routines (functions) for the store's order management manager.

#######################################################################
#
# AgoraCart and all associated files, except where noted, are
# Copyright 2001 to Present jointly by K-Factor Technologies, Inc.
# and by C E Mayo (aka Mister Ed) at AgoraCart.com & K-Factor.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This copyright notice may not be removed or altered in any way.
#
#######################################################################

{
    local ($modname) = 'order_management';
    register_extension( $modname, 'Order Management', $versions{$modname} );
    register_menu( 'display_Order_mgr_screen', 'display_Order_mgr_screen',
        $modname, 'Order Management - Store Settings' );
    add_settings_choice(
        'Order Management Settings',
        ' Order Management Settings ',
        'display_Order_mgr_screen'
    );
    register_menu( 'OrderManagementSettings', 'write_Order_mgr_settings',
        $modname, 'Write Order Settings' );
    add_ordermanage_settings_choice(
        'View New Orders',
        ' View New Orders ',
        'order_log'
    );
    add_ordermanage_settings_choice(
        'Order Management Settings',
        ' Order Management Settings ',
        'display_Order_mgr_screen'
    );

}

#######################################################################

sub write_Order_mgr_settings {
    local ($myset) = q{};

    $myset .= "\$sc_order_log_name = \"$in{'name_of_the_log_file'}\";\n";

    $in{'name_of_the_log_file'} =~ /([\w\-\/\.\:]+)/;
    $sc_order_log_name = $1;
    if ( !(-f "$sc_logs_dir/$sc_order_log_name") ) {
        open (ORDLOG, ">>", "$sc_logs_dir/$sc_order_log_name") || update_error_log("File Open Error: $!", 'Order_Manager-ext_lib.pl', 'write_Order_mgr_settings sub-routine at: open new order log file');
        print ORDLOG "";
        close ORDLOG;
    }
    $myset .= "\$sc_send_order_to_log = \"$in{'log_orders_yes_no'}\";\n";
    $myset .=
"\$sc_write_individual_order_logs = \"$in{'sc_write_individual_order_logs'}\";\n";
    $myset .=
"\$sc_write_monthly_master_order_logs = \"$in{'sc_write_monthly_master_order_logs'}\";\n";
    $myset .=
"\$sc_write_monthly_short_order_logs = \"$in{'sc_write_monthly_short_order_logs'}\";\n";
    $myset .=
"\$sc_write_product_sales_logs = \"$in{'sc_write_product_sales_logs'}\";\n";
    $myset .=
      "\$sc_defaultProductStatus = \"$in{'sc_defaultProductStatus'}\";\n";
    $myset .= "\$exportNumberOfFields = \"$in{'exportNumberOfFields'}\";\n";
    $myset .=
      "\$sc_order_status_default = \"$in{'sc_order_status_default'}\";\n";
    $myset .= "\$sc_item_Total_Weight = \"$in{'sc_item_Total_Weight'}\";\n";

    update_store_settings( 'ORDERMANAGER', $myset );
    display_Order_mgr_screen();
}

#######################################################################

sub display_Order_mgr_screen {

    if ( $sc_item_Total_Weight eq '' ) {
        $sc_item_Total_Weight = "item Total Weight";
    }

    if ( $sc_defaultProductStatus eq '' ) {
        $sc_defaultProductStatus = 'in Stock';
    }

    if ( $exportNumberOfFields eq '' ) {
        $exportNumberOfFields = 30;
    }

    my $temp_if_exporter_exists = q{};
    if ( -e "$mgrlibdir/Order_Exporter-ext_lib.pl" ) {
        $temp_if_exporter_exists = qq|
<p><hr></p>

<p><b>Number of Exporter Fields to Display: &nbsp; <a data-toggle="collapse" href="#exporterFields" aria-expanded="false" aria-controls="exporterFields"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="exportNumberOfFields" type="number" class="form-control" size="2" maxlength="2" value="$exportNumberOfFields"></p>
<div class="collapse" id="exporterFields">
     <div class="well">
         <p>This is the number of fields to display in the Custom Order Export Manager.</p>
     </div>
</div>
|;
    }

    print &$manager_page_header( 'Order Management Configuration', '', '', '', '' );

    print <<ENDOFTEXT;
<h1 class='mgr-page-title'><b>Order Logging &amp; Sales Reporting Configuration</b></h1>
<br><br>
<p class="well">This order management configuration area allows you to select which order logs you wish to use as well as a few other defaults used in the order logs. Click on the <i class="fa fa-info-circle"></i> icon next to any setting for more information on the setting.</p>
<p><br></p>
ENDOFTEXT

    if ( $in{'system_edit_success'} ) {
        print <<ENDOFTEXT;
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 hidden-xs">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bg-success">
        <h4 class="text-primary mgr-center">Your Order Management settings have been successfully updated</h4>
    </div>
</div>
<div class="row col-xs-12 col-sm-12"><p><br></p></div>
ENDOFTEXT
    }

    print qq~
<div class="row col-xs-12 col-sm-12">
<form action="manager.cgi" method="post"><input type="hidden" name="mgr_seshun" value="$in{'mgr_seshun'}">

<p><b>Write orders to a temporary New Orders log file? &nbsp; <a data-toggle="collapse" href="#useNewOrdersLog" aria-expanded="false" aria-controls="useNewOrdersLog"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name="log_orders_yes_no">
<option>$sc_send_order_to_log</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="useNewOrdersLog">
     <div class="well">
         <p>Default is YES.</p>
         <p>This is an order log that acts as a staging area NEW orders with very basic order details for all new orders.</p>
         <p>It is recommended that you delete orders that have been shipped and have been completed from this order log.  Order details are still retained in individual and monthly order files by default.</p>
         <p>Unless you have a good reason to set this to no, it is recommended that you leave it set to YES.</p>
         <p>LEGACY NOTE: this is the original order log for versions circa version 5.x. The purpose explanation has been refined as many users were still using it as a main order log and not deleting orders as they were fulfilled (or by some other criteria). Letting this file grow over months and years can slow performance, so we recommend pruning the orders listed as they are not needed anymore. Archived orders can be searched for by number, or month and year.
     </div>
</div>
<p><br></p>

<p><b>Choose a unique name for your new orders log file: (ex: "mylog3218.log")</b><span style="padding-left: 25px;"></span><input name="name_of_the_log_file" type="text" class="form-control" value="$sc_order_log_name"></p>
<p><hr></p>

<p><b>Do you wish to have the orders written to a individual order log files? &nbsp; <a data-toggle="collapse" href="#useIndivOrdersLog" aria-expanded="false" aria-controls="useIndivOrdersLog"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name="sc_write_individual_order_logs">
<option>$sc_write_individual_order_logs</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="useIndivOrdersLog">
     <div class="well">
         <p>Default is YES.</p>
         <p>This is an order log that stores an individual order with complete details. A new log is created for each order completed by a customer.</p>
         <p>You can use it with/or without the Master order log file.</p>
         <p>This is he main order log used to generate invoices, packing slips and the order update/edit screens.</p>
         <p>Unless you have a good reason to set this to no, it is recommended that you leave it set to YES.</p>
     </div>
</div>
<p><hr></p>

<p><b>Do you wish to have the orders written to a monthly overview order log file? &nbsp; <a data-toggle="collapse" href="#useMonthlyOverview" aria-expanded="false" aria-controls="useMonthlyOverview"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name="sc_write_monthly_short_order_logs">
<option>$sc_write_monthly_short_order_logs</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="useMonthlyOverview">
     <div class="well">
         <p>Default is YES.</p>
         <p>This is an order log that stores only basic but key data on each and every order within a calandar month.</p>
         <p>We recommend this be set to YES if you think you might ever want to use any custom sales/order reports managers in the future or other tools used for quick order lookups & reports. Only data stored in these log files will be available to such managers and cannot be repopulated later, so setting to YES is advised, just in case.</p>
     </div>
</div>
<p><hr></p>

<p><b>Do you wish to have the orders written to a monthly master order log file? &nbsp; <a data-toggle="collapse" href="#useMonthlyMaster" aria-expanded="false" aria-controls="useMonthlyMaster"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name="sc_write_monthly_master_order_logs">
<option>$sc_write_monthly_master_order_logs</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="useMonthlyMaster">
     <div class="well">
         <p>Default is NO.</p>
         <p>This is an order log that stores complete data on each and every order within a calandar month.</p>
         <p>We recommend this be set to YES if you ever want to use any data export managers in the future for such things as exporting data for accounting purposes or other.</p>
         <p>Individual log files contain the same information, but are not as handy if you wish to export a large number of orders to accounting and/or spreadsheet formats. So set to YES if you use our order exporter module.</p>
         <p><b>CAUTION:</b> these files can be very substantial in size if you get a large number of orders, so use with care.</p>
     </div>
</div>
<p><hr></p>

<p><b>Do you wish to keep track of products solds? &nbsp; <a data-toggle="collapse" href="#useAnnualProdLog" aria-expanded="false" aria-controls="useAnnualProdLog"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name="sc_write_product_sales_logs">
<option>$sc_write_product_sales_logs</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="useAnnualProdLog">
     <div class="well">
         <p>Default is YES.</p>
         <p>This is an order log that records the totals for each item sold in your product database.</p>
         <p>This is a very simple logging tool. Refunds and order cancelations are not reflected in the totals.</p>
         <p> A nice little tool to see the year to date sales of each item you offer.</p>
         <p>Logging is perfermed on an annual basis, so a new order log file is created upon the first order of the year.</p>
     </div>
</div>
<p><hr></p>

<p><b>Title for item shipping weight/cost: &nbsp; <a data-toggle="collapse" href="#shippingFieldTitle" aria-expanded="false" aria-controls="shippingFieldTitle"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="sc_item_Total_Weight" type="text" class="form-control" maxlength="30" value="$sc_item_Total_Weight"></p>
<div class="collapse" id="shippingFieldTitle">
     <div class="well">
         <p>This is a title for order management displays, invoices, and packing slips for the value found in each of your product's shipping weight/cost field within your product database.</p>
         <p>Select a title appropriate to your useage of this field.</p>
         <p><b>Example</b> if the field is used to indicate an item's weight, this is then totalled up for the total weight of each individual product sold (which is used store wide and cannot be used both ways, must be a weight, a cost or blank).  So, this will be the title for the output for the order management functions in regards to this product data field.</p>
     </div>
</div>
<p><hr></p>

<p><b>Enter Default Order Status for New Orders: &nbsp; <a data-toggle="collapse" href="#newOrderStatus" aria-expanded="false" aria-controls="newOrderStatus"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name="sc_order_status_default">
<option>$sc_order_status_default</option>
$scm_orderStatusDropList
</select></p>
<div class="collapse" id="newOrderStatus">
     <div class="well">
         <p>This is the default wording used for each order's status when initially received.</p>
         <p>This is an order log that records the totals for each item sold in your product database.</p>
         <p>This is a very simple logging tool. Refunds and order cancelations are not reflected in the totals.</p>
         <p> A nice little tool to see the year to date sales of each item you offer.</p>
         <p>Logging is perfermed on an annual basis, so a new order log file is created upon the first order of the year.</p>
     </div>
</div>
<p><hr></p>

<p><b>Enter Default Product Status: &nbsp; <a data-toggle="collapse" href="#productDefaultStatus" aria-expanded="false" aria-controls="productDefaultStatus"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="sc_defaultProductStatus" type="text" class="form-control" maxlength="30" value="$sc_defaultProductStatus"></p>
<div class="collapse" id="productDefaultStatus">
     <div class="well">
         <p>This is the default wording used for each product as it's written to the logs, unless it's downloadable (the download module is a members only add-on file).</p>
     </div>
</div>

$temp_if_exporter_exists

<p><br></p>
<input type="hidden" name="system_edit_success" value="yes">
<input name="OrderManagementSettings" type="hidden" value="Submit">
<button class="btn btn-primary btn-lg mgr-center" style="display: block;">Submit Settings Updates</button>
<p><br><br><br></p>
</form>
</div>
~;

    print &$manager_page_footer;
}

#######################################################################

1;
