Momscode - MVS Manufacturing Code Logics
Rewinding Work Order
Overview The RewindingWorkOrder class is a custom DocType in Frappe that manages rewinding work orders. It includes several methods to validate data, fetch stock quantities, and create associated records.
Methods and Their Functions validate(self)
Function: This method is called during the validation phase before saving the RewindingWorkOrder document. It performs two main checks:
Overlapping Date and Time Check: Ensures that no other Rewinding Work Order exists with an overlapping date and time range. Uses a filter to find records where the start_date_and_time is before the current document's end_date_and_time and the end_date_and_time is after the current document's start_date_and_time, excluding the current document itself. Raises an error if any overlapping records are found. Raw Material Quantity Check: Verifies that the quantity of each raw material in the raw_materials table does not exceed the available quantity in the specified warehouse. Retrieves the latest stock ledger entry for each item and compares it against the required quantity. Raises an error if the required quantity exceeds the available quantity. get_available_qty(self, item_code, warehouse)
Function: Fetches the available quantity of a specific item in a given warehouse.
Queries the Stock Ledger Entry to get the most recent entry for the specified item and warehouse. Returns the available quantity or 0 if no entries are found. fetch_cost(self)
Function: Retrieves the cost per kilogram for the current workstation.
Queries the Moms Custom Settings to get the cost associated with the current workstation. Returns the cost value found or 0 if no matching workstation is found in the settings. get_available_qtys(self, item_code, warehouse)
Function: Similar to get_available_qty, this method fetches the available quantity of an item in a warehouse.
This method appears redundant as it performs the same function as get_available_qty. Ensure only one version of this method is kept. create_rewinding_process(self)
Function: Creates a new Rewinding Job Card based on the details in the current Rewinding Work Order.
Creates a new Rewinding Job Card document and sets its fields using the data from the current work order. Appends items and scrap data to the job card, including quantities and prices. Calculates the required price per kilogram and adds this information to the rewinding_items table. Saves the new job card and updates the status of the current work order to "Created".
RewindingWorkOrder Class Functions 1. validate(self) Purpose: Ensures data integrity and consistency before saving the document.
Key Checks:
Date and Time Overlap: Prevents creation of overlapping work orders. Raw Material Quantity: Confirms that required quantities do not exceed available stock. Exceptions:
Throws an error if overlaps are found or if raw material quantities exceed available stock. 2. get_available_qty(self, item_code, warehouse) Purpose: Retrieves the available quantity of a specific item from a given warehouse.
Usage: Provides the current stock level to ensure that the required quantity can be fulfilled.
Returns: The available quantity or 0 if no stock ledger entry is found.
- fetch_cost(self) Purpose: Gets the cost per kilogram for a specified workstation from custom settings.
Usage: Used to calculate costs related to the rewinding process.
Returns: The cost per kilogram for the workstation or 0 if no cost is found.
- get_available_qtys(self, item_code, warehouse) Purpose: Fetches the available quantity of an item in a warehouse (similar to get_available_qty).
Usage: Provides the stock level for verification.
Returns: The available quantity or 0 if no stock ledger entry is found.
- create_rewinding_process(self) Purpose: Creates a Rewinding Job Card document based on the current work order details.
Steps:
Initializes a new job card document. Sets fields using data from the work order. Appends item and scrap information. Calculates and assigns prices. Saves the job card and updates the work order status. Outcome: Creates a new job card and updates the status of the work order to "Created".
Rewinding Job Card
Documentation for Rewinding Job Card Custom Scripts Overview The provided custom scripts enhance the Rewinding Job Card DocType functionality in Frappe/ERPNext by implementing various validations, calculations, and dynamic field updates. The scripts ensure that data integrity is maintained and provide useful features like automatic calculations and user alerts.
Code Functions and Features 1. update_table Function Purpose: Updates the product_specification table and performs related calculations. Details: Iterates through the product_specification table and updates each row. Calls calculate_area_and_feet to update area and dimensions. Updates the production_item field for each row. Calculates the total weight and additional cost. Updates the consumed_qty for the main raw material if it matches an item in rewinding_items. Displays a green alert message indicating the table has been updated. 2. validate Function Purpose: Validates form data before saving. Details: Checks that all items in the rewinding_items table with serial or batch numbers have a batch number. Sets the length_in_meter, width_in_meter, area_in_square_meter, and their feet equivalents to zero if weight_in_kg is zero. Ensures length_in_meter and width_in_meter are greater than zero if weight_in_kg is not zero. 3. refresh Function Purpose: Refreshes form fields and sets up custom buttons and filters. Details: Adds a "Create Serial No" button if no QR code or serial number exists and there is a production item. Adds a "Create QR" button if QR code is missing but serial numbers exist. Sets filters for batch field based on item code and purpose. Filters workstation field based on production_line. Filters employee field in employee_list to show only active employees. 4. total_duration Function Purpose: Calculates the total duration between start_time and end_time. Details: Computes duration in hours, minutes, and seconds. Sets the total duration field and refreshes related fields. Displays an error if the end time is earlier than the start time. 5. calculate_totals_durationsss Function Purpose: Calculates the total duration for all entries in the actual_time table. Details: Sums up all durations in seconds and converts the total to hours, minutes, and seconds. Updates the total_time field with the formatted duration. 6. calculate_area_and_feet Function Purpose: Calculates the area in square meters and square feet based on length and width. Details: Converts length and width from meters to feet. Computes and sets the area in square meters and square feet. 7. calculate_total_weight Function Purpose: Calculates the total weight from the product_specification table. Details: Sums up the source_item_weight_in_kg for all rows. Sets the total weight and calculates additional costs. 8. calculate_total_additional_cost Function Purpose: Calculates additional costs based on total weight. Details: Multiplies the total weight by the additional cost and sets the result. Rewinding Actual Time DocType start_time and end_time Events Purpose: Triggers duration calculations when start_time or end_time fields are updated. Details: Calls total_duration and calculate_totals_durationsss to update the total duration. actual_time_remove Event Purpose: Updates the total duration when an entry is removed. Details: Calls calculate_totals_durationsss to recalculate total duration. Rewinding Items DocType item_code Event Purpose: Validates item selection and checks for duplicates. Details: Verifies if the item belongs to allowed item groups. Ensures that items from the same group are not duplicated in other rows. batch Event Purpose: Validates batch number selection. Details: Ensures selected batch is not already used in another row. Employee List DocType employee Event Purpose: Ensures no duplicate employees are selected. Details: Checks if the selected employee is already present in the list and clears the duplicate entry. Rewinding Scrap Items DocType qty and rate Events Purpose: Calculates the amount for scrap items based on quantity and rate. Details: Updates the amount field when qty or rate is changed. Job Card Finished Items DocType product_specification_add Event Purpose: Updates the number field and recalculates total weight when a new row is added. Details: Sets the number field based on the row index and refreshes fields. length_in_meter and width_in_meter Events Purpose: Calculates area and feet dimensions when length or width is updated. Details: Calls calculate_area_and_feet to update area and dimensions. product_specification_remove Event Purpose: Updates the number field and recalculates total weight when a row is removed. Details: Sets the number field based on the last row index and recalculates total weight.
- create_qr_code The function generates QR codes for each product specification and saves the image as a file. Suggestion: Ensure that the QR code image file names are unique and handle potential file overwrites by using the row.idx value.
- create_batch This function creates a new batch ID if one doesn't exist and assigns it to the product specification. Suggestion: Consider adding error handling if the Batch document fails to submit. Also, ensure that batch_id is unique to avoid potential conflicts.
- before_save Validates overlapping shifts and assigns production_item to each product specification. Suggestion: Ensure that self.is_new() is used properly and that the validation logic accurately reflects your requirements.
- before_submit Checks if actual_time and employee_list are provided before submission. Suggestion: Uncomment and validate the weight check if it’s necessary.
- on_submit Creates a Stock Entry, updates the Rewinding Work Order, and links the stock entry with the job card. Suggestions: Ensure that the stock entry fields match the intended functionality. Consider adding exception handling in case any of the operations fail. Review the logic for updating the Rewinding Work Order to ensure it's correct and accounts for all relevant cases.
- on_cancel Updates the status of the Rewinding Work Order when the job card is canceled. Suggestion: Uncomment the necessary lines if they are intended to handle quantities or other adjustments.
- on_trash Updates the Rewinding Work Order status when the job card is trashed. Suggestion: Similar to on_cancel, uncomment the necessary logic if adjustments are needed for quantities or related documents. Additional Notes: Ensure that the file path handling for QR codes (/public/files/) and the File doctype are correct and accessible. Review the database queries and ensure they are optimized for performance. Consider adding logging or error handling to capture any issues during execution.