Saturday, 28 April 2018

How to set naming custom series

Date.prototype.getMonthName = function(lang)
 {
    lang = lang && (lang in Date.locale) ? lang : 'en';
    return Date.locale[lang].month_names[this.getMonth()];
};

Date.prototype.getMonthNameShort = function(lang)
 {
    lang = lang && (lang in Date.locale) ? lang : 'en';
    return Date.locale[lang].month_names_short[this.getMonth()];
};

Date.locale = {
    en: {
       month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
       month_names_short: ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
    }
};

frappe.ui.form.on("Purchase Order", "validate", function(frm) {
  item_code = 'ERP'
  frappe.call({
          method: "frappe.client.get_value",
                async:false,
          args: {
              doctype: "Item",
              fieldname: "item_code",
              filters: { name:cur_frm.doc.items[0].item_code },
          },
          callback: function(res){
              if (res && res.message){
            item_code=res.message['item_code']
                         
              }
          } 
      });
  var ymd = cur_frm.doc.transaction_date;
  var yy = ymd.substring(-1,4);
  var mm = ymd.substring(5,7);
  var d = new Date(cur_frm.doc.transaction_date)
  var ym = "PO"+"-"+ yy +"-"+ item_code + "-.###";
  frm.set_value("naming_series", ym);
})

Write a script for calculate total working hours in java script

in_time= cur_frm.doc.in_time;
out_time=cur_frm.doc.out_time;
cur_frm.set_value("new_in_time",in_time);
cur_frm.set_value("new_out_time",out_time);

    var start = in_time.split(':');
    var end = out_time.split(':');

    var start_hours = start[0];
    var end_hours = end[0];

    var start_minute = start[1];
    var end_minute = end[1];

    var start_second = start[2];
    var end_second = end[2];

    var total_minute = parseInt(start_minute) + parseInt(end_minute);

    var total_second = parseInt(start_second) + parseInt(end_second);

    total = 0;
    if (start_hours > end_hours){
      var start_hours= 24-start_hours;
      total=parseInt(start_hours)  + parseInt(end_hours);
    }
    else{
      total = end_hours - start_hours;
    }
    if(start_minute>=1){
      if(end_minute > start_minute){
        total_minute = end_minute - start_minute;
      }
      else{
        total_minute = 60  - start_minute ;
      }
      end_minute = (60 - start_minute) + parseInt(end_minute);
      if(end_minute < 60){
        total = total - 1;
      }
      if ( end_minute < 0){
        total_minute = 60 - end_minute;
      }
    }
    if(total_minute >=60){
      total_minute = total_minute - 60;
        }
    if(total_second >= 60){
      total_second = total_second - 60;
      total_minute = total_minute +1;
    }
    minute_length = total_minute.toString().length;
    total_length = total.toString().length;;
    if(minute_length==1){
      total_minute = "0" + total_minute;
    }
    if(total_length==1){

      total = "0" + total;
    }
   
    total= total +":"+ total_minute ;
    frm.set_value("total_working_hours", total);

How to get item from Quotation with check box or user choice.

frappe.ui.form.on("Sales Order", "refresh", function(frm, doctype, name) {
cur_frm.add_custom_button(__('Get Individual Items'), function() {

var d = new frappe.ui.Dialog({
  title: __("Get Item from Quote"),
  fields: [
      {
        "label": __("Quotation"),
        "fieldname": "quotation",
        "fieldtype": "Link",
        "options": "Quotation",
        "reqd": 1
      },
      {
        "fieldname":"sb1",
        "fieldtype":"Section Break",
      },
      {
        "label": __("html"),
        "fieldname": "my_html",
        "fieldtype": "HTML",
        "options" : "Items from quote<br>",
      },

      ],
      primary_action_label: "Get Items",
      primary_action: function(){
       //msgprint("hi");
       args = d.get_values();
       if(!args) return;
       console.log(args.quotation);
       quotation_name = args.quotation;
       all_items = []
       frappe.call({
           method: "frappe.client.get_list",
           args: {
               doctype: "Quotation Item",
               fields: ["item_code", "item_name", "qty", "rate","name"],
               filters: { 'parent':  quotation_name },
           },
              callback: function(res){
                 if (res && res.message){
                      all_items = res.message;
                      html = "Items from quote<br>"
                      console.log( res.message);
                      for (var i = 0; i<res.message.length; i=i+1) {
                        console.log( res.message[i].item_code);
                            html +="<p><input type='checkbox' class='select' id='_select' name='"
                            +res.message[i].item_code
                            +"' value='"
                            +res.message[i].item_code
                            +"'> "
                            +res.message[i].item_code
                            + "</p>"
                      }
                      html += "<br><input type='button'name='Add items' value='Add items' <br>"
                      $("#item_select").append("sam");
                      var wrapper = d.fields_dict.my_html.$wrapper;
                      wrapper.empty();
                      wrapper.html(html);
                  }
              }   
       });
     
     }
 });
d.show();

var test_list = [];

$(".frappe-control input:checkbox:checked").each ( function() {
  test_list.push($(this).attr("name"));
});

test_list.forEach(function(element) {
    console.log(element);
    var row = frappe.model.add_child(cur_frm.doc, "Sales Order Item", "items");
    row.item_code = element;
    row.item_name = element;
    row.rate = 0;
    //add child
});
 refresh_field("items");

});
});

Django rest api - filter

views.py from django_filters.rest_framework import DjangoFilterBackend class PollList(viewsets.ModelViewSet):     queryset = X.objects...