[
 {
  "docstatus": 0,
  "doctype": "Client Script",
  "dt": "Serial No",
  "enabled": 1,
  "modified": "2026-05-19 09:16:00",
  "module": null,
  "name": "Serial No - Price Calculator",
  "script": "frappe.ui.form.on('Serial No', {\n    grade: function(frm) {\n        calculate_recommended_price(frm);\n    },\n    cpu_test: function(frm) {\n        check_hardware_failures(frm);\n    },\n    ram_test: function(frm) {\n        check_hardware_failures(frm);\n    },\n    refresh: function(frm) {\n        // Label change via JS (since fixture label updates need migrate)\n        var price_field = frm.get_field('assigned_price');\n        if (price_field && price_field.$wrapper) {\n            price_field.$wrapper.find('.control-label').text('Recommended Price');\n        }\n        check_hardware_failures(frm);\n    }\n});\n\nfunction check_hardware_failures(frm) {\n    var cpu_fail = frm.doc.cpu_test === 'Fail';\n    var ram_fail = frm.doc.ram_test === 'Fail';\n    \n    if (cpu_fail || ram_fail) {\n        // Force Flagged grade and dismantle routing (but allow reviewer override)\n        if (!frm.doc.grade || frm.doc.grade !== 'Flagged') {\n            frm.set_value('grade', 'Flagged');\n        }\n        frm.set_value('assigned_price', null);\n        frm.set_value('pricing_status', 'Dismantle');\n        \n        var reason = [];\n        if (cpu_fail) reason.push('CPU test failed');\n        if (ram_fail) reason.push('RAM test failed');\n        \n        // Show warning banner instead of locking the field\n        frm.set_intro(\n            '<i class=\"fa fa-exclamation-triangle\"></i> <strong>Hardware Failure:</strong> ' + \n            reason.join(', ') + ' — routed to Dismantle. Change tests to Pass to re-enable pricing.',\n            'red'\n        );\n    } else {\n        // Clear the warning banner\n        frm.clear_intro();\n        // If grade was Flagged but tests now pass, let user manually change grade\n    }\n}\n\nfunction calculate_recommended_price(frm) {\n    var grade = frm.doc.grade;\n    \n    // Flagged = no price, show FLAGGED text\n    if (!grade || grade === 'Flagged') {\n        frm.set_value('assigned_price', null);\n        frm.set_value('pricing_status', grade === 'Flagged' ? 'Flagged' : 'Needs Pricing');\n        return;\n    }\n    \n    // Need item reference to get market prices\n    if (!frm.doc.item_code) {\n        return;\n    }\n    \n    frappe.call({\n        method: 'frappe.client.get',\n        args: {\n            doctype: 'Item',\n            name: frm.doc.item_code\n        },\n        callback: function(r) {\n            if (!r.message) return;\n            var item = r.message;\n            var base_price = 0;\n            var price_source = '';\n            \n            switch(grade) {\n                case 'High':\n                    base_price = item.market_high || item.base_market_price || 0;\n                    price_source = 'market_high';\n                    break;\n                case 'Med':\n                    base_price = item.market_median || item.base_market_price || 0;\n                    price_source = 'market_median';\n                    break;\n                case 'Low':\n                    base_price = item.market_low || item.base_market_price || 0;\n                    price_source = 'market_low';\n                    break;\n            }\n            \n            if (base_price > 0) {\n                frm.set_value('assigned_price', Math.round(base_price * 100) / 100);\n                frm.set_value('pricing_status', 'Priced');\n                frm.set_value('pricing_source', price_source);\n            } else {\n                frm.set_value('assigned_price', null);\n                frm.set_value('pricing_status', 'Needs Pricing');\n            }\n        }\n    });\n}\n",
  "view": "Form"
 },
 {
  "docstatus": 0,
  "doctype": "Client Script",
  "dt": "Pallet",
  "enabled": 1,
  "modified": "2026-05-17 05:39:15.497479",
  "module": null,
  "name": "Pallet - View Serials",
  "script": "frappe.ui.form.on('Pallet', {\n    refresh: function(frm) {\n        frm.add_custom_button(__('View Serials'), function() {\n            frappe.set_route('List', 'Serial No', {\n                'pallet': frm.doc.pallet_number || frm.doc.name\n            });\n        }, __('View'));\n        \n        frm.add_custom_button(__('Serials Spreadsheet'), function() {\n            frappe.set_route('query-report', 'Serial Nos by Pallet', {\n                'pallet': frm.doc.pallet_number || frm.doc.name\n            });\n        }, __('View'));\n    }\n});\n",
  "view": "Form"
 },
 {
  "docstatus": 0,
  "doctype": "Client Script",
  "dt": "Load",
  "enabled": 1,
  "modified": "2026-05-17 05:39:15.577871",
  "module": null,
  "name": "Load - View Pallets Button",
  "script": "frappe.ui.form.on('Load', {\n    refresh: function(frm) {\n        frm.add_custom_button(__('View Pallets'), function() {\n            frappe.set_route('List', 'Pallet', {'load': frm.doc.name});\n        }, __('Actions'));\n    }\n});\n",
  "view": "Form"
 },
 {
  "docstatus": 0,
  "doctype": "Client Script",
  "dt": "Scheduled Pickup",
  "enabled": 1,
  "modified": "2026-05-17 05:39:15.588395",
  "module": null,
  "name": "Scheduled Pickup - Auto Fill",
  "script": "frappe.ui.form.on('Scheduled Pickup', {\n    customer_number: function(frm) {\n        var customer = frm.doc.customer_number;\n        if (!customer) {\n            clear_supplier_fields(frm);\n            return;\n        }\n        frappe.call({\n            method: 'frappe.client.get',\n            args: {doctype: 'Supplier', name: customer},\n            callback: function(r) {\n                if (!r.message) return;\n                var s = r.message;\n                if (!frm.doc.company_name && s.supplier_name) {\n                    frm.set_value('company_name', s.supplier_name);\n                }\n                if (s.supplier_primary_contact) {\n                    frappe.call({\n                        method: 'frappe.client.get',\n                        args: {doctype: 'Contact', name: s.supplier_primary_contact},\n                        callback: function(cr) {\n                            if (!cr.message) return;\n                            var ct = cr.message;\n                            var full_name = [ct.first_name, ct.last_name].filter(Boolean).join(' ');\n                            if (!frm.doc.contact_name) frm.set_value('contact_name', full_name);\n                            if (!frm.doc.contact_phone) frm.set_value('contact_phone', ct.phone || '');\n                            if (!frm.doc.contact_email) frm.set_value('contact_email', ct.email_id || '');\n                        }\n                    });\n                }\n                if (s.supplier_primary_address) {\n                    frappe.call({\n                        method: 'frappe.client.get',\n                        args: {doctype: 'Address', name: s.supplier_primary_address},\n                        callback: function(ar) {\n                            if (!ar.message) return;\n                            var a = ar.message;\n                            if (!frm.doc.address_line) frm.set_value('address_line', a.address_line1 || '');\n                            if (!frm.doc.city) frm.set_value('city', a.city || '');\n                            if (!frm.doc.state) frm.set_value('state', a.state || '');\n                            if (!frm.doc.zip_code) frm.set_value('zip_code', a.pincode || '');\n                        }\n                    });\n                }\n                if (s.geocoded && s.latitude && s.longitude) {\n                    frm.set_value('latitude', s.latitude);\n                    frm.set_value('longitude', s.longitude);\n                    frm.set_value('geocoded', 1);\n                }\n            }\n        });\n    }\n});\n\nfunction clear_supplier_fields(frm) {\n    frm.set_value('company_name', '');\n    frm.set_value('contact_name', '');\n    frm.set_value('contact_phone', '');\n    frm.set_value('contact_email', '');\n    frm.set_value('address_line', '');\n    frm.set_value('city', '');\n    frm.set_value('state', '');\n    frm.set_value('zip_code', '');\n    frm.set_value('latitude', '');\n    frm.set_value('longitude', '');\n    frm.set_value('geocoded', 0);\n}\n",
  "view": "Form"
 },
 {
  "docstatus": 0,
  "doctype": "Client Script",
  "dt": "System Pricing",
  "enabled": 1,
  "modified": "2026-05-17 05:41:31.790905",
  "module": null,
  "name": "System Pricing - Age Indicators",
  "script": "frappe.listview_settings['System Pricing'] = {\n    add_fields: [\"scraped_at\", \"days_since_pricing\", \"pricing_status\"],\n    get_indicator: function(doc) {\n        if (doc.pricing_status === \"Needs Pricing\" || doc.pricing_status === \"Error\") {\n            return [__(\"Needs Pricing\"), \"orange\", \"pricing_status,=,Needs Pricing\"];\n        }\n        if (!doc.scraped_at) {\n            return [__(\"No Date\"), \"gray\", \"scraped_at,is,not set\"];\n        }\n        var days = doc.days_since_pricing || 0;\n        if (days < 90) {\n            return [__(\"Fresh\"), \"green\", \"days_since_pricing,<,90\"];\n        } else if (days < 120) {\n            return [__(\"Aging\"), \"yellow\", \"days_since_pricing,<,120\"];\n        } else {\n            return [__(\"Expired\"), \"red\", \"days_since_pricing,>=,120\"];\n        }\n    }\n};",
  "view": "List"
 },
 {
  "docstatus": 0,
  "doctype": "Client Script",
  "dt": "System Pricing",
  "enabled": 1,
  "modified": "2026-05-17 05:41:31.954814",
  "module": null,
  "name": "System Pricing - Auto Calculate Days",
  "script": "frappe.ui.form.on('System Pricing', {\n    refresh: function(frm) {\n        calculate_days(frm);\n    },\n    scraped_at: function(frm) {\n        calculate_days(frm);\n    }\n});\n\nfunction calculate_days(frm) {\n    if (frm.doc.scraped_at) {\n        var scraped = new Date(frm.doc.scraped_at);\n        var now = new Date();\n        var diffTime = Math.abs(now - scraped);\n        var diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));\n        frm.set_value('days_since_pricing', diffDays);\n    } else {\n        frm.set_value('days_since_pricing', 0);\n    }\n}",
  "view": "Form"
 }
]