码迷,mamicode.com
首页 > 其他好文 > 详细

qqqqqqq

时间:2015-12-10 19:24:53      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:

{{//<?
    //order_uid=cgidata.order_uid;
    //order_uid=1127;
    %include "/extenso/functions/sn_pages.snc";
    %include "/extenso/functions/sn_ct.snc";
    %include "/functions/client.sn";
    use lg;
//    use widget; // to know widget information
    use uid; // uid of the widget
    use table; // sn_widget
    use days;
}}
\{{
    use session_id;
    use cgidata;
    use request;
    use extranet_user;
    use check_schedule;

    cgidata = cgidata();

    if cgidata.uids ne "" then
        // in case there are more than one order selected, we use only the first one
        uids=explode(",", cgidata.uids);
        order_uid=uids[0];
    //else
    //    "Please select an order";
    //    exit(0);
    endif
    sum_price = 0;

    order_info = sql(single:true,"select * from mtm_order where uid = ‘?‘",order_uid);
}}

<html>
<head>
<!-- <link href="/css/en/styles.css" rel="stylesheet"> -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">


</head>
<body>
<div class="rows">
    <div class="container">
    <h1>{{sn_ct("Order Detail", allpages:true, edit:true)}}</h1>
    <form class="form-horizontal order-verification" method="post" onsubmit="return saveData()">
        <input type="hidden" name="order_uid" value="\{{order_uid}}">
        <fieldset>
            <legend>{{sn_ct("Customer No.:", allpages:true, edit:true)}}&nbsp;&nbsp;
                    \{{ extranet_user.uid }}</legend>
            <legend>{{sn_ct("Address", allpages:true, edit:true)}}</legend>
            <div class="form-group" id="step4-address">
                <div class="col-xs-12 col-sm-6">
                    <div class="row">
                        <div class="col-xs-6"><strong>{{sn_ct("Billing Address", allpages:true, edit:true)}}</strong></div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Company:", allpages:true, edit:true)}}</div>
                        <div class="col-xs-6">\{{ order_info.rows.billing_company }}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Address:", allpages:true, edit:true)}}</div>
                        <div class="col-xs-6">\{{ order_info.rows.order_billing_address }}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("City:", allpages:true, edit:true)}}</div>
                        <div class="col-xs-6">\{{ order_info.rows.order_billing_city }}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Province:", allpages:true, edit:true)}}</div>
        \{{
            if order_info.rows.order_billing_state ne "" then
                if order_info.rows.order_billing_country ne ‘US‘ && order_info.rows.order_billing_country ne ‘CA‘ then
                    province_description = order_info.rows.order_billing_state;
                else
                    province = sql(single:true,"select description_{{lg.rows.lg}} as description from mtm_province where province_id = ‘?‘", order_info.rows.order_billing_state);
                    province_description = province.rows.description;
                endif
            else
                province_description = "--";
            endif
        }}
                        <div class="col-xs-6">\{{ province_description;}}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Postal Code(ZIP):", allpages:true, edit:true)}}</div>
                        <div class="col-xs-6">\{{ order_info.rows.order_billing_postalcode }}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Country:", allpages:true, edit:true)}}</div>
        \{{
            if order_info.rows.order_billing_country ne "" then
                country = sql(single:true,
                    "select description_{{lg.rows.lg}} as description from mtm_country where country_id=‘?‘",
                    order_info.rows.order_billing_country);
                country_description = country.rows.description;
            else
                country_description = "--";
            endif
        }}
                        <div class="col-xs-6">\{{ country_description }}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Telephone:", allpages:true, edit:true)}}</div>
                        <div class="col-xs-6">\{{ order_info.rows.order_billing_tel }}&nbsp;&nbsp;
                            <span style="color:#000;">{{sn_ct("Ext.", allpages:true, edit:true)}}</span>
                                \{{ order_info.rows.order_billing_number_ext }}
                        </div>
                    </div>
                </div>
                <div class="col-xs-12 col-sm-6">
                    <div class="row">
                        <div class="col-xs-6"><strong>{{sn_ct("Delivery Address", allpages:true, edit:true)}}</strong></div>
                        <div class="col-xs-6"></div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Company:", allpages:true, edit:true)}}</div>
                        <div class="col-xs-6">\{{ order_info.rows.delivery_company }}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Address:", allpages:true, edit:true)}}</div>
                        <div class="col-xs-6">\{{ order_info.rows.order_delivery_address }}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("City:", allpages:true, edit:true)}}</div>
                        <div class="col-xs-6">\{{ order_info.rows.order_delivery_city }}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Province:", allpages:true, edit:true)}}</div>
        \{{
            if order_info.rows.order_delivery_state ne "" then
                if order_info.rows.order_delivery_country ne ‘US‘ && order_info.rows.order_delivery_country ne ‘CA‘ then
                    province_description = order_info.rows.order_delivery_state;
                else
                    province = sql(single:true,"select description_{{lg.rows.lg}} as description from mtm_province where province_id = ‘?‘", order_info.rows.order_billing_state);
                    province_description = province.rows.description;
                endif
            else
                province_description = "--";
            endif
        }}
                        <div class="col-xs-6">\{{province_description}}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Postal Code(ZIP):", allpages:true, edit:true)}}</div>
                        <div class="col-xs-6">\{{ order_info.rows.order_delivery_postalcode }}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Country:", allpages:true, edit:true)}}</div>
        \{{
            if order_info.rows.order_billing_country ne "" then
                country = sql(single:true,
                    "select description_{{lg.rows.lg}} as description from mtm_country where country_id=‘?‘",
                    order_info.rows.order_delivery_country);
                country_description = country.rows.description;
            else
                country_description = "--";
            endif
        }}
                        <div class="col-xs-6">\{{ country_description }}</div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6">{{sn_ct("Telephone:", allpages:true, edit:true)}}</div>
                        <div class="col-xs-6">\{{ order_info.rows.order_delivery_tel }}&nbsp;&nbsp;
                            <span style="color: #000;">{{sn_ct("Ext.", allpages:true, edit:true)}}
                            </span>\{{ order_info.rows.order_delivery_number_ext }}
                        </div>
                    </div>

                </div>
            </div>
        </fieldset>
        \{{ if order_info.nbrows > 0 then }}
        <fieldset>
            <legend>{{sn_ct("Product Detail", allpages:true, edit:true)}}</legend>
            <div class="form-group">
                <div class="col-xs-12">
                    <table class="table">
                        <thead>
                        <tr class="text-center">
                            <th>{{sn_ct("# Product", allpages:true, edit:true)}}</th>
                            <th>{{sn_ct("Name", allpages:true, edit:true)}}</th>
                            <th>{{sn_ct("Qty<br>ordered", allpages:true, edit:true)}}</th>
                            <th>{{sn_ct("Qty<br>Stock", allpages:true, edit:true)}}</th>
                            <th style="text-align:center;">{{sn_ct("Regular<br>Price", allpages:true, edit:true)}}</th>
                            <th style="text-align:center;">{{sn_ct("Special<br>Price", allpages:true, edit:true)}}</th>
                            <th style="text-align:right;">{{sn_ct("Total", allpages:true, edit:true)}}</th>
                            <th style="text-align:right;">{{sn_ct("Operate", allpages:true, edit:true)}}</th>
                        </tr>
                        </thead>
                        <tbody>
                        \{{
                            sql_order_detail = "select * from mtm_order_detail where order_id = ‘?‘";
                            for order_product sql(sql_order_detail,order_uid) do
                                //if price ...
                                sum_price = sum_price + order_product.rows.product_price * order_product.rows.quantity_ordered;
                                // to get the stock
                                sql_product_numb = "select product_number from mtm_product where product_id = ‘?‘";
                                product_numb = sql(single:true,sql_product_numb,order_product.rows.product_id);
                                if product_numb.rows.product_number eq "" then
                                    product_numb.rows.product_number = 0;
                                endif
                                // get the relative pgroup_id
                                sql_pgroup_product = "select pgroup_id,product_id from mtm_pgroup_product where product_id = ‘?‘";
                                pg_pro_id = sql(single:true,sql_pgroup_product,order_product.rows.product_id);

                        }}
                        <tr data-uid="\{{ order_product.rows.uid }}" class="text-left item">
                            <input type="hidden" name="row\{{ order_product.rows.uid }}" value="\{{ order_product.rows.uid }}" >
                            <td>
                                <a href="{{ pages(‘product_detail‘, lg.rows.lg) }}\{{ ‘?pgroup_id=‘; pg_pro_id.rows.pgroup_id }}">\{{order_product.rows.product_id}}</a>
                            </td>
                            <td>\{{order_product.rows.product_name}}</td>
                            <td><input class="form-control input-sm input-qty" style="width:80px;" type="number" min="0" name="row\{{ order_product.rows.uid }}_qty" value="\{{ order_product.rows.quantity_ordered }}" ></td>
                            <td>\{{ product_numb.rows.product_number }}</td>
                            <td align="center">
                                <div class="input-group">
                                  <span class="input-group-addon">$</span>
                                  <input type="text" class="form-control input-sm price" style="width:100px;text-align:right;" value="\{{ number_format(order_product.rows.product_price,‘en‘) }}" >
                                  <input type="hidden" class="price-hidden" name="row\{{ order_product.rows.uid }}_price" value="\{{ order_product.rows.product_price }}" >
                                </div>
                            </td>
                            \{{ if order_product.rows.product_special_price ne "0" then }}
                            <td align="center">
                                <div class="input-group">
                                  <span class="input-group-addon">$</span>
                                  <input type="text" class="form-control input-sm spec-price" style="width:100px;text-align:right;" value="\{{ number_format(order_product.rows.product_special_price,‘en‘) }}" >
                                  <input type="hidden" class="spec-price-hidden" name="row\{{ order_product.rows.uid }}_spec_price" value="\{{ order_product.rows.product_special_price }}" >
                                </div>
                            </td>
                            \{{ else }}
                            <td align="center">
                                <div class="input-group">
                                  <span class="input-group-addon">$</span>
                                  <input type="text" class="form-control input-sm spec-price" style="width:100px;text-align:right;" name="row\{{ order_product.rows.uid }}_spec_price" value="\{{ number_format(order_product.rows.product_special_price,‘en‘) }}" >
                                  <input type="hidden" class="spec-price-hidden" name="row\{{ order_product.rows.uid }}_spec_price" value="\{{ order_product.rows.product_special_price }}" >
                                </div>
                            </td>
                            \{{ endif }}
                            \{{ if order_product.price ne "0" then }}
                            <td align="right">$&nbsp;<span class="sub-total">\{{ number_format(order_product.rows.product_price*order_product.rows.quantity_ordered,"en") }}</span></td>
                            \{{ else
                                have_price_on_request="*";}}
                            <td align="right"><span class="glyphicon glyphicon-earphone mtm-icon font-size-2em" title="Price On Request"></span></td>
                            \{{ endif }}
                            <td align="center"><span class="glyphicon glyphicon-remove product-remove" title="Remove product"></span></td>
                        </tr>
                        <tr class="attribution">
                            \{{
                                sql_description = "select trim(leading ‘0‘ from av.description_{{lg.rows.lg}}) as descs
                                        from    mtm_pgroup_product pgp,
                                                mtm_product_attribute_value pav,
                                                mtm_attribute_value av,
                                                mtm_pgroup_attribute pa
                                        WHERE pgp.pgroup_id = ‘?‘
                                        AND pav.product_id = ‘?‘
                                        AND pgp.product_id = pav.product_id
                                        AND pav.attribute_value_id = av.attribute_value_id
                                        AND pgp.pgroup_id = pa.pgroup_id
                                        AND pa.attribute_id = av.attribute_id
                                        order by pa.attribute_order";
                            //    if sql_pgroup_product_flag.nbrows ne 0 then
                            }}
                            <td style="border:0"></td>
                            <td style="border:0" colspan="6">
                            \{{
                                // list pgroup description values
                                    separator = "";
                                    for attribute sql(sql_description,pg_pro_id.rows.pgroup_id,pg_pro_id.rows.product_id) do
                                        separator;
                                        attribute.rows.descs;
                                        separator = " - ";
                                    endfor
                            //    endif
                            }}
                            </td>
                        </tr>
                        \{{
                            endfor
                        }}
                        </tbody>
                        <tfoot>
                        <tr>
                            <td colspan="8" align="right">
                                <strong>{{ sn_ct(edit:false, "Sub-Total: ") }}</strong>$&nbsp;<span id="sum_price">\{{ number_format(sum_price,‘en‘) }}</span>
                            </td>
                        </tr>

                        \{{
                            delivery_postal_code = substr(order_info.rows.order_delivery_postalcode,0,1);
                            sql_delivery_cost = "select * from mtm_delivery_cost where postal_code = ‘?‘";
                            delivery_costs = sql(single:true,sql_delivery_cost,delivery_postal_code);
                            if sum_price le delivery_costs.rows.free_from then
                                delivery_money = 0;
                            else
                                delivery_money = delivery_costs.rows.delivery_cost;
                            endif
                            cost_with_delivery = sum_price + delivery_money;
                        }}
                        <tr>
                            <td colspan="4"></td>
                            <td nowrap="nowrap" colspan="2">{{sn_ct("Delivery cost", allpages:true, edit:true)}}</td>
                            <td nowrap="nowrap" colspan="2">
                                <div class="input-group" style="margin-left:100px;">
                                  <span class="input-group-addon">$</span>
                                  <input type="text" id="delivery_cost" class="form-control input-sm" style="width:100px;text-align:right;" name="delivery_cost" value="\{{ number_format(delivery_money,‘en‘) }}" >
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4"></td>
                            <td nowrap="nowrap" colspan="2">{{sn_ct("Total without taxes", allpages:true, edit:true)}}</td>
                            <td nowrap="nowrap" colspan="2" class="text-right">$&nbsp;<span id="total_without_tax">\{{number_format(cost_with_delivery, "{{lg.rows.lg}}");}}</span></td>
                            <input type="hidden" id="order_subtotal" name="order_subtotal" value="\{{cost_with_delivery}}">
                        </tr>
                        {{
                                tax_canada = sn_ct("GST", allpages:true, edit:false);
                                tax_province = sn_ct("PST", allpages:true, edit:false);
                                tax_combined = sn_ct("HST", allpages:true, edit:false);
                        }}
                        \{{
                            cost_tps = 0;
                            cost_tvq = 0;
                            cost_tvh = 0;
                            province_tax = sql(single:true,"select tps,tvq,tvh from mtm_province where province_id=‘?‘ and country_code=‘?‘", order_info.rows.order_delivery_state, order_info.rows.order_delivery_country);

                            if order_info.rows.order_billing_country eq ‘CA‘ then
                                if province_tax.rows.tvh eq ‘0‘ || province_tax.rows.tvh eq ‘‘ then
                                    cost_tps = cost_with_delivery*province_tax.rows.tps;
                                    cost_tvq = cost_with_delivery*province_tax.rows.tvq;
                        }}
                        <tr>
                        {{
                            // canada tax. On the screen you show "GST" in english and "TPS" in french
                        }}
                            <td colspan="4"></td>
                            <td nowrap="nowrap" colspan="2">{{tax_canada}}</td>
                            <td nowrap="nowrap" colspan="2" class="text-right">$&nbsp;<span class="tax-canada">\{{number_format(cost_tps, "{{lg.rows.lg}}");have_price_on_request;}}</span></td>
                            <input type="hidden" name="order_tps" id="tax_canada" value="\{{cost_tps}}">
                        </tr>
                        <tr>
                        {{
                            // province tax. On the screen you show "PST" in english and "TVQ" in french
                        }}
                            <td colspan="4"></td>
                            <td nowrap="nowrap" colspan="2">{{tax_province}}</td>
                            <td nowrap="nowrap" colspan="2" class="text-right">$&nbsp;<span class="tax-province">\{{number_format(cost_tvq, "{{lg.rows.lg}}");have_price_on_request;}}</span></td>
                            <input type="hidden" name="order_tvq" id="tax_province" value="\{{cost_tvq}}">
                        </tr>
                            \{{
                                // combined tax. On the screen you show "HST" in english and "TVH" in french
                                else
                                    cost_tvh = cost_with_delivery*province_tax.rows.tvh;
                            }}
                        <tr>
                            <td colspan="4"></td>
                            <td nowrap="nowrap" colspan="2">{{tax_combined}}</td>
                            <td nowrap="nowrap" colspan="2" class="text-right">$&nbsp;<span class="tax-combined">\{{number_format(cost_tvh, "{{lg.rows.lg}}");have_price_on_request;}}</span></td>
                            <input type="hidden" name="order_tvh" id="tax_combined" value="\{{cost_tvh}}">
                        </tr>
                        \{{
                                endif
                            endif
                            total_cost = cost_with_delivery + cost_tps + cost_tvq + cost_tvh;
                        }}
                        <tr>
                            <td colspan="4"></td>
                            <td nowrap="nowrap" colspan="2">{{sn_ct("Total with taxes", allpages:true, edit:true)}}</td>
                            <td nowrap="nowrap" colspan="2" class="text-right">$&nbsp;<span class="total-with-tax">\{{number_format(total_cost, "{{lg.rows.lg}}");have_price_on_request;}}</span></td>
                            <input type="hidden" id="order_total" name="order_total" value="\{{total_cost}}">
                        </tr>

                        <tr>
                            <td colspan="8" align="right">
                                <div class="alert alert-success" style="display:none">
                                  <strong>Success!</strong> Saved the data successfully !
                                </div>
                                <div class="alert alert-danger" style="display:none">
                                  <strong>Danger!</strong> An error occured, try again !
                                </div>
                                <a id="addProduct" class="btn btn-default btn-md" href="javascript:void(0);" data-toggle="modal" data-target="#myModal" style="width: 15%">
                                    {{ sn_ct(edit:false, "Add product") }}
                                    <span class="glyphicon glyphicon-shopping-cart"></span>
                                </a>
                                <button class="btn btn-default btn-md" type="submit" style="width: 15%">
                                    {{sn_ct("Save",edit:false)}}
                                    <span class="glyphicon glyphicon-save"></span>
                                </button>
                            </td>
                        </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </fieldset>
        \{{ endif }}

    </form>
    </div>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog" style="width: 60%">
      <div class="modal-content">
         <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true" id="close2" style="float:right;margin-left:10px;">
                   <span>&times;</span>
            </button>
         </div>
         <div class="modal-body">
          <span style="margin:0;padding:0;"><h3>{{ sn_ct("Add a product") }}</h3></span>
            <input type="text" id="product_id" class="form-control" placeholder="{{ sn_ct(edit:false, "product_id Search") }}">
            <table class="product_detail_tab modal-body">
                <tbody id="product-tbody">
                </tbody>
           </table>
        </div>

         <div class="modal-footer" id="close1">
            <button type="button" class="btn btn-default"
               data-dismiss="modal">{{ sn_ct(edit:false, "Close") }}
            </button>
         </div>
    </div>
</div>
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script type="text/javascript">

    var items = [];
    // updata total price
    function updateTotal()
    {
        var subTotals = 0;
        for(var i=0,k=items.length;i<k;i++){
        //    console.log("updateTotal for:"+items[i].subTotal);
            subTotals = parseFloat(subTotals) + parseFloat(items[i].subTotal);
        }
        subTotals = parseFloat(subTotals).toFixed(2);
        //console.log("updateTotal1:"+subTotals);
        $("#sum_price").html(fmoney(subTotals,2));
        //console.log("updateTotal2:"+fmoney(subTotals,2));
        // update the total cost
        var deliveryCost = parseFloat($("#delivery_cost").val());
        TotalWithoutTax = parseFloat(subTotals) + parseFloat(deliveryCost);
        $("#total_without_tax").html(fmoney(TotalWithoutTax,2));
        $("#order_subtotal").val(parseFloat(TotalWithoutTax).toFixed(2));
        updateTax();
    }

    // update the tax
    function updateTax(){
        // get the subtotal
        var subTotal = parseFloat($("#order_subtotal").val());
        // get province id
        var provinceId = "\{{order_info.rows.order_delivery_state}}";
        // ajax - get the taxes
        $.ajax({
            type: "post",
            url: "http://smtm.sednove.ca/modules/products/ressources/en/ajax_calculate_tax.snc",
            data: {"province_id":provinceId,"subtotal":subTotal},
            dataType: "json",
            success: function(data){
                var gst = data.gst;
                var pst = data.pst;
                var hst = data.hst;
                var total = data.total;
                // set the values showing in the page
                $(".tax-canada").html(fmoney(gst));
                $(".tax-province").html(fmoney(pst));
                $(".tax-combined").html(fmoney(hst));
                $(".total-with-tax").html(fmoney(total,2));
                // set the input:hidden values
                $("#tax_canada").val(gst);
                $("#tax_province").val(pst);
                $("#tax_combined").val(hst);
                $("#order_total").val(total);
            }
        });
    }
    // when delivery changed
    function deliveryChange(){
        var sumPrice = parseFloat($("#sum_price").text().replace(",",""));
        var deliveryCost = $("#delivery_cost").val();
        if(!$.isNumeric(deliveryCost)){
            deliveryCost = 0;
            $("#delivery_cost").val("0.00");
        }
        if(deliveryCost < 0.01){
            deliveryCost = 0;
            $("#delivery_cost").val("0.00");
        }
        var TotalWithoutTax = parseFloat(sumPrice) + parseFloat(deliveryCost);
        $("#total_without_tax").html(fmoney(TotalWithoutTax,2));
        $("#order_subtotal").val(parseFloat(TotalWithoutTax).toFixed(2));
        updateTax();
    }
    // save the data
    function saveData(){
        //ajax
        $form = $("form");
        $.ajax({
            type: "post",
            url: "/modules/products/ressources/en/ajax_admin_modify_order.snc",
            data: $form.serialize(),
            dataType: "json",
            success: function(data){
                var $succ = $(".alert-success");
                var $err = $(".alert-danger");
                $succ.hide();
                $err.hide();
                if(data){
                    $succ.fadeIn("normal");
                }else{
                    $err.fadeIn("normal");
                }
            }
        });
        return false;
    }
    // money format
    function fmoney(s, n) {  
        n = n > 0 && n <= 20 ? n : 2;  
        s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";  
        var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];  
        t = "";
        for (i = 0; i < l.length; i++) {  
            t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");  
        }  
        return t.split("").reverse().join("") + "." + r;  
    }

    $(document).ready(function(){
        // bind event
        $("#delivery_cost").on("change",deliveryChange);
        //$("#submitButton").on("click",saveData);
        // the index of the product items
        var item_index = 0;
        $(".table").find(‘tbody tr.item‘).each(function(){
            var $tr_item = $(this);
            var item = {};
            var item_uid = item_index++;
            $tr_item.data("item_uid",item_uid);
            var $qtyElement = $tr_item.find(".input-qty");
            var $priceElement = $tr_item.find(".price");
            var $specPriceElement = $tr_item.find(".spec-price");
            var $subTotalElement = $tr_item.find(".sub-total");
            var $priceHidden = $tr_item.find(".price-hidden");
            var $specPriceHidden = $tr_item.find(".spec-price-hidden");
            // set the relative values
            item.qty = $qtyElement.val();
            item.price = $priceHidden.val();
            item.specPrice = $specPriceHidden.val();
            //item.subTotal = item.qty*item.price;
            if(item.specPrice && item.specPrice > 0){
                item.subTotal = parseFloat(item.specPrice*item.qty).toFixed(2);
            }else{
                item.subTotal = parseFloat(item.price*item.qty).toFixed(2);
            }
            // add this json(item) to the array
            items.push(item);
            //set change event to the tr items
            $tr_item.on(‘change‘, function(){
                // get the values again
                item.qty = $qtyElement.val();
                item.price = $priceElement.val().replace(",","");
                item.specPrice = $specPriceElement.val().replace(",","");
                //check if it is a number
                if(!$.isNumeric(item.qty)){
                    item.qty = 0;
                    $qtyElement.val("0");
                }
                if(!$.isNumeric(item.price)){
                    item.price = 0;
                    $priceElement.val("0.00");
                }
                if(!$.isNumeric(item.specPrice)){
                    item.specPrice = 0;
                    $specPriceElement.val("0.00");
                }
                if(item.price < 0.01) item.price = 0;
                if(item.specPrice < 0.01) item.specPrice = 0;
                // compare price and special price
                if(item.specPrice && item.specPrice > 0){
                    item.subTotal = parseFloat(item.specPrice*item.qty).toFixed(2);
                }else{
                    item.subTotal = parseFloat(item.price*item.qty).toFixed(2);
                }
                // change the values and format
                $priceElement.val(fmoney(item.price,2));
                $specPriceElement.val(fmoney(item.specPrice,2));
                $priceHidden.val(item.price);
                $specPriceHidden.val(item.specPrice);
                //console.log("Hidden:"+$priceHidden.val()+":"+$specPriceHidden.val());
                $subTotalElement.html(fmoney(item.subTotal,2));
                var itemVal = $tr_item.data("item_uid");
                items[itemVal].subTotal = item.subTotal.replace(",","");
                //console.log("items[itemVal].subTotal:"+items[itemVal].subTotal);
                // updata total price
                updateTotal();
            });
        });

        // remove a product
        $(".product-remove").each(function(){
            $(this).on("click",function(){
                // confrim
                var removeFlag = confirm("Are you sure to remove this product ?");
                if(removeFlag){
                    var $trProduct = $(this).closest("tr");
                    var productUid = $trProduct.data("uid");
                    console.log("product uid:"+productUid);
                    // ajax remove the product
                    $.ajax({
                        type: "post",
                        url: "/modules/products/ressources/en/ajax_admin_remove_product.snc",
                        data: {"product_uid":productUid},
                        dataType: "json",
                        success: function(data){
                            if(data){
                                console.log("product remove:"+data);
                                // remove the relative product element
                                var $trNext = $trProduct.next();
                                if($trNext.hasClass("attribution")){
                                    $trNext.remove();
                                }
                                $trProduct.remove();
                                // reset the price
                                updateTotal();
                                // if no product,set the delivery cost = 0
                                if(!$("#order_subtotal").val()){
                                    $("#delivery_cost").val("0.00");
                                }

                            }else{
                                console.log("product remove error:"+data);
                            }
                            return false;
                        }
                    });
                }
            });
        });
    });
    // search product
    $("#close1").click(function() {
        location.reload();
    });
    $("#close2").click(function() {
        location.reload();
    });

    var timer = 0;
    $("#product_id").on("keyup",function(){
        var product_id = $("#product_id").val();
        var product_id_len = product_id.length;
        if(product_id_len > 2) {
            clearTimeout(timer);
            timer = setTimeout(function(){
                // ajax - get the products through product id
                $.ajax({
                    type: "post",
                    data: {"inp":product_id},
                    url: "/modules/products/ressources/{{lg.rows.lg}}/ajax_add_to_cart_admin.snc",
                    dataType: ‘html‘,
                    success:function(data){
                    //    console.log("data:"+data);
                        var $new_body = $(data);
                        $("#product-tbody").empty().append($new_body);
                    }
                });
            }, 500);
        }
    });

    function addProductCart(obj,productId,price,specSprice,productTitle){
    //    console.log("add to product cart");
        var orderId = \{{order_uid}};
        var idString = $(obj).attr(‘id‘);
        var idArray = idString.split("_");
        var idValue = idArray[2];
        var qtyValue = $("#mtm_qty_"+idValue).val();
        console.log(orderId+":"+productId+":"+price+":"+specSprice+":"+qtyValue+":"+productTitle);
        $.ajax({
            type: "post",
            url: "/modules/products/ressources/{{lg.rows.lg}}/ajax_admin_add_product.snc",
            data: { "order_id": orderId,
                    "product_id": productId,
                    "product_price": price,
                    "product_special_price": specSprice,
                    "quantity_ordered": qtyValue,
                    "product_name": productTitle
                },
            dataType: ‘json‘,
            success: function(data)
            {
                $(obj).addClass("added-to-cart");
                console.log("success:"+data);
            },
            error: function(data)
            {
                console.log(‘error on ajax call‘);
            }
        });
    }
</script>
<link rel="stylesheet" type="text/css" href="/extenso/css/style.css"/>
<style>
.font-size-2em {
    font-size: 2em;
    cursor: pointer;
}
.attribution {
    color: #009683;
}
.product-remove,.added-to-cart {
    cursor: pointer;
    color: red;
}
</style>
</body>
</html>

qqqqqqq

标签:

原文地址:http://www.cnblogs.com/hzj680539/p/5036934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!