DELIMITER $$ CREATE PROCEDURE `CallStrikePlanned`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select COUNT(customer_visits.id) as visitshop from customer_visits left join customer_infos on customer_infos.user_id = customer_visits.customer_id where customer_visits.is_sequnece = "1" AND (customer_visits.shop_status = "open" '; IF org_id > 0 THEN SET otherreplace = ' AND customer_visits.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.created_at between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `CallStrikeVisited`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select COUNT(customer_visits.id) as visitshop from customer_visits left join customer_infos on customer_infos.user_id = customer_visits.customer_id where (customer_visits.is_sequnece = "1" OR customer_visits.is_sequnece = "0") AND (customer_visits.shop_status = "open" '; IF org_id > 0 THEN SET otherreplace = ' AND customer_visits.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND date(customer_visits.created_at) between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' )'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `CarryOverReport`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8, IN `users_id` VARCHAR(500) CHARSET utf8) BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'SELECT salesman_unloads.id,salesman_unloads.trip_id,salesman_unloads.transaction_date ,users.firstname, users.lastname,salesman_infos.user_id,salesman_infos.salesman_code,routes.route_code,routes.route_name,items.id as item_id,items.item_code,items.item_name,salesman_unload_details.unload_qty,salesman_unloads.code, item_uoms.name as item_uom_name, item_uoms.code as item_uoms_code,item_uoms.id as item_uoms_id from salesman_unloads LEFT JOIN salesman_unload_details on salesman_unload_details.salesman_unload_id = salesman_unloads.id LEFT JOIN items ON items.id = salesman_unload_details.item_id LEFT JOIN item_uoms ON item_uoms.id = salesman_unload_details.item_uom LEFT JOIN users on users.id = salesman_unloads.salesman_id LEFT JOIN salesman_infos on salesman_infos.user_id = salesman_unloads.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id LEFT JOIN routes on routes.id = salesman_unloads.route_id where users.deleted_at is null and salesman_unloads.organisation_id = org_ids '; SET SQLText = REPLACE (SQLText,'org_ids',org_id); IF users_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(users.id, "useridss")'; SET otherreplace = REPLACE (otherreplace,'useridss',users_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND salesman_unloads.transaction_date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_unloads.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' GROUP BY salesman_unloads.trip_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `CarryOverReportDetails`(IN `unload_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'SELECT items.id as item_id,items.item_code,items.item_name,salesman_unload_details.unload_qty, item_uoms.name as item_uom_name, item_uoms.code as item_uoms_code,item_uoms.id as item_uoms_id from salesman_unload_details LEFT JOIN items ON items.id = salesman_unload_details.item_id LEFT JOIN item_uoms ON item_uoms.id = salesman_unload_details.item_uom'; IF unload_id > 0 THEN SET otherreplace = ' WHERE salesman_unload_details.salesman_unload_id = unload_idss '; SET otherreplace = REPLACE (otherreplace,'unload_idss',unload_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `CustomerSalesPerMonthReport`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cha_id` VARCHAR(500) CHARSET utf8, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8, IN `cust_id` INT(11) UNSIGNED) BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select customer_infos.id, customer_infos.user_id as customer_user_id, customer_infos.customer_code, users.firstname, users.lastname, users.email, users.mobile, customer_types.customer_type_name as customer_type, customer_groups.group_name as customer_group, invoices.invoice_date, salesman_infos.salesman_supervisor, salesman_infos.user_id, customer_infos.region_id, customer_routes.route_id, customer_lobs.lob_id FROM customer_infos left join users on customer_infos.user_id = users.id left join customer_types on customer_infos.customer_type_id = customer_types.id left join customer_groups on customer_infos.customer_group_id = customer_groups.id left join invoices on customer_infos.user_id = invoices.customer_id left join salesman_infos on invoices.salesman_id = salesman_infos.user_id left join customer_lobs on customer_infos.id = customer_lobs.customer_info_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id left join customer_routes on customer_infos.id = customer_routes.customer_id WHERE customer_infos.organisation_id = org_ids and date(invoices.invoice_date) between "sdatess" and "edatess" '; IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF org_id > 0 THEN SET SQLText = REPLACE (SQLText,'org_ids',org_id); END IF; IF cha_id != '' THEN SET otherreplace = ' AND customer_infos.channel_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cha_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id > 0 THEN SET otherreplace = ' AND customer_infos.user_id = cust_ids'; SET otherreplace = REPLACE (otherreplace,'cust_ids',cust_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND invoices.salesman_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,all_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND customer_lobs.lob_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,div_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND customer_infos.region_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,region_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND customer_routes.route_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,route_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,supervisor_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,salesman_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_infos.id, users.firstname, users.lastname, users.email, users.mobile, customer_type, customer_group '; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `DailyFieldActivityReport`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8, IN `start_limit` INT(11) UNSIGNED, IN `end_limit` INT(11) UNSIGNED) BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select salesman_infos.user_id,salesman_infos.id, salesman_infos.created_at, salesman_infos.user_id, salesman_infos.salesman_code,users.firstname,users.lastname FROM salesman_infos left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id LEFT JOIN users on users.id = salesman_infos.user_id where salesman_infos.deleted_at is null '; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = orgidss'; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' GROUP BY salesman_infos.user_id ORDER BY salesman_infos.created_at'; SET SQLText = CONCAT(SQLText,otherreplace); IF start_limit > 0 AND end_limit <= 0 THEN SET otherreplace = ' LIMIT end_limits , start_limits'; SET otherreplace = REPLACE (otherreplace,'start_limits',start_limit); SET otherreplace = REPLACE (otherreplace,'end_limits',end_limit); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `GetTotalReturnCreditNote`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select credit_notes.id as credit_notes_id from credit_notes left join salesman_infos on salesman_infos.user_id = credit_notes.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor WHERE credit_notes.credit_note_date between "sdatess" and "edatess" '; IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF org_id > 0 THEN SET otherreplace = ' AND credit_notes.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND credit_notes.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `GetTotalReturnCreditNoteDetails`(IN `credit_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select SUM(credit_note_details.item_qty) as Total_return_qty from credit_note_details left join items on items.id = credit_note_details.item_id '; IF credit_id != '' THEN SET otherreplace = ' WHERE credit_note_details.credit_note_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,credit_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `GetTotalSalesInvoices`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select invoices.id as invoices_id FROM invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor '; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' WHERE invoices.invoice_date between "sdatess" and "edatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `GetTotalSalesInvoicesDetails`(IN `invoice_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select SUM(invoice_details.item_qty + invoice_details.item_price) as Total_sales_qty FROM invoice_details left join items on items.id = invoice_details.item_id left join item_uoms on item_uoms.id = items.lower_unit_uom_id '; IF invoice_id != '' THEN SET otherreplace = ' WHERE invoice_details.invoice_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,invoice_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `GetTotalSalesInvoicesDetailsQty`(IN `invoice_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select SUM(invoice_details.item_qty) as Total_sales_qty FROM invoice_details left join items on items.id = invoice_details.item_id left join item_uoms on item_uoms.id = items.lower_unit_uom_id '; IF invoice_id != '' THEN SET otherreplace = ' WHERE invoice_details.invoice_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,invoice_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `GetTotalSalesInvoicesPerDate`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select invoices.id as invoices_id FROM invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor '; IF sdate IS NOT NULL THEN SET otherreplace = ' WHERE invoices.invoice_date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `MonthlyAgeingReport`(IN `sdate` DATE, IN `edate` DATE, IN `cust_id` INT(11) UNSIGNED, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8, IN `org_id` INT(11) UNSIGNED) BEGIN DECLARE SQLText varchar(8000); DECLARE otherreplace varchar(500); SET SQLText ='select customer_infos.customer_code, concat(users.firstname," ",users.lastname) AS customer_fullname, customer_infos.id, customer_infos.user_id, invoices.invoice_date, invoices.pending_credit as invoice_pending_credit, credit_notes.pending_credit as credit_notes_pending_credit, customer_infos.route_id as customer_infos_route_id, customer_lobs.route_id as customer_lobs_route_id, customer_lobs.lob_id, YEAR(invoices.invoice_date) as year, MONTH(invoices.invoice_date) as month, MONTHNAME(invoices.invoice_date) as monthname, sum(invoices.pending_credit) as invoice_total, sum(credit_notes.pending_credit) as credit_notes_total from customer_infos left join users on customer_infos.user_id = users.id left join invoices on customer_infos.user_id = invoices.customer_id left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id left join customer_lobs on customer_infos.id = customer_lobs.customer_info_id left join credit_notes on users.id = credit_notes.customer_id WHERE invoices.invoice_date is not null AND invoices.pending_credit is not null AND credit_notes.pending_credit is not null AND invoices.invoice_date between "sdatess" and "edatess" '; IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF cust_id > 0 THEN SET otherreplace = ' AND customer_infos.user_id = cust_ids'; SET otherreplace = REPLACE (otherreplace,'cust_ids',cust_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND customer_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_infos.id, month, year order by customer_infos.id asc'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `MonthlyAgeingReportOneCustomer`(IN `sdate` DATE, IN `edate` DATE, IN `cust_id` INT(11) UNSIGNED, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8, IN `org_id` INT(11) UNSIGNED) BEGIN DECLARE SQLText varchar(8000); DECLARE otherreplace varchar(500); SET SQLText ='select invoices.pending_credit as invoice_pending_credit from customer_infos left join users on customer_infos.user_id = users.id left join customer_routes on customer_infos.id = customer_routes.customer_id left join invoices on customer_infos.user_id = invoices.customer_id left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id WHERE invoices.invoice_date is not null AND invoices.pending_credit is not null AND invoices.invoice_date between "sdatess" and "edatess" '; IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF org_id > 0 THEN SET otherreplace = ' AND customer_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id > 0 THEN SET otherreplace = ' AND customer_infos.user_id = cust_ids'; SET otherreplace = REPLACE (otherreplace,'cust_ids',cust_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `MonthlyAgeingReportTestOne`(IN `cust_id` INT(11) UNSIGNED, IN `org_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE SQLText LONGTEXT; DECLARE otherreplace LONGTEXT; SET SQLText ='select customer_infos.customer_code, concat(users.firstname," ",users.lastname) AS customer_fullname, customer_infos.id, customer_infos.user_id from customer_infos left join users on customer_infos.user_id = users.id left join customer_lobs on customer_infos.id = customer_lobs.customer_info_id'; IF cust_id > 0 THEN SET otherreplace = ' AND customer_infos.user_id = cust_ids'; SET otherreplace = REPLACE (otherreplace,'cust_ids',cust_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND customer_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_infos.id order by customer_infos.id asc'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `MonthlyAgeingReportTestThree`(IN `user_id` INT(11) UNSIGNED, IN `org_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE SQLText LONGTEXT; DECLARE otherreplace LONGTEXT; SET SQLText ='select credit_notes.pending_credit as credit_notes_pending_credit,sum(credit_notes.pending_credit) as credit_notes_total from credit_notes '; IF user_id > 0 THEN SET otherreplace = ' WHERE credit_notes.customer_id = cust_ids'; SET otherreplace = REPLACE (otherreplace,'cust_ids',user_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND credit_notes.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `MonthlyAgeingReportTestTwo`(IN `sdate` DATE, IN `edate` DATE, IN `userids` LONGTEXT CHARSET utf8, IN `org_id` INT(11) UNSIGNED, IN `all_ids` LONGTEXT CHARSET utf8, IN `div_this` LONGTEXT CHARSET utf8, IN `region_this` LONGTEXT CHARSET utf8, IN `route_this` LONGTEXT CHARSET utf8, IN `supervisor_this` LONGTEXT CHARSET utf8, IN `salesman_this` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE SQLText LONGTEXT; DECLARE otherreplace LONGTEXT; SET SQLText ='select invoices.invoice_date, invoices.pending_credit as invoice_pending_credit, YEAR(invoices.invoice_date) as year, MONTH(invoices.invoice_date) as month, MONTHNAME(invoices.invoice_date) as monthname, sum(invoices.pending_credit) as invoice_total from invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id WHERE invoices.invoice_date is not null AND invoices.pending_credit is not null AND invoices.invoice_date between "sdatess" and "edatess" '; IF userids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,userids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by month, year'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `PerodicewisecollectionReport`(IN `sdate` DATE, IN `edate` DATE, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8, IN `org_id` INT(11) UNSIGNED) BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select users.firstname,users.lastname,customer_infos.customer_code,collections.collection_number as recipt_number,collections.payemnt_type,invoices.invoice_number,collection_details.amount,collection_details.pending_amount,collections.created_at as date from collections left join salesman_infos on salesman_infos.user_id = collections.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id left join users on users.id = collections.customer_id left join customer_infos on customer_infos.user_id = users.id left join collection_details on collection_details.collection_id = collections.id left join invoices on invoices.id = collection_details.invoice_id WHERE customer_infos.customer_code IS NOT NULL'; IF sdate IS NOT NULL AND edate IS NOT NULL THEN SET otherreplace = ' AND collections.created_at between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND collections.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(collections.salesman_id, "saelsidss")'; SET otherreplace = REPLACE (otherreplace,'saelsidss',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' Group BY date'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `PerodicewisecollectionReportPDC`(IN `sdate` DATE, IN `edate` DATE, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8, IN `org_id` INT(11) UNSIGNED) BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select users.firstname,users.lastname,customer_infos.customer_code,collections.cheque_number,collections.cheque_date,collections.collection_number as recipt_number,collections.payemnt_type,invoices.invoice_number,collection_details.amount,collection_details.pending_amount,collections.created_at as date from collections left join salesman_infos on salesman_infos.user_id = collections.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id left join users on users.id = collections.customer_id left join customer_infos on customer_infos.user_id = users.id left join collection_details on collection_details.collection_id = collections.id left join invoices on invoices.id = collection_details.invoice_id WHERE customer_infos.customer_code IS NOT NULL AND collection_status = "PDC" '; IF sdate IS NOT NULL AND edate IS NOT NULL THEN SET otherreplace = ' AND collections.created_at between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND collections.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(collections.salesman_id, "saelsidss")'; SET otherreplace = REPLACE (otherreplace,'saelsidss',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' Group BY date'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `SalesQuantityCreditNoteReport`(IN `sdate` DATE, IN `edate` DATE, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `cat_id` VARCHAR(500) CHARSET utf8, IN `cust_id` VARCHAR(500) CHARSET utf8, IN `org_id` INT(11) UNSIGNED, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8) BEGIN DECLARE ids varchar(500); DECLARE cates_id varchar(500); DECLARE other varchar(500); DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET cates_id = REPLACE(cat_id,' ',''); SET other = REPLACE(all_ids,' ',''); SET SQLText ='select credit_notes.credit_note_number,credit_notes.id as credit_notes_id, credit_notes.salesman_id, credit_notes.customer_id, credit_note_details.id as credit_note_details_id, credit_note_details.item_id, items.item_code, items.item_name,item_uoms.name as uoms_name, SUM(credit_note_details.item_qty) as Total_return_qty from credit_notes left join credit_note_details on credit_note_details.credit_note_id = credit_notes.id left join items on items.id = credit_note_details.item_id left join item_uoms on item_uoms.id = items.lower_unit_uom_id left join salesman_infos on salesman_infos.user_id = credit_notes.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id WHERE items.organisation_id = org_idss AND credit_notes.credit_note_date between "sdatess" and "edatess" '; IF org_id > 0 THEN SET SQLText = REPLACE (SQLText,'org_idss',org_id); END IF; IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(credit_notes.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',other); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cat_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(items.item_major_category_id, "cates_idss")'; SET otherreplace = REPLACE (otherreplace,'cates_idss',cates_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(credit_notes.customer_id, "cust_ids")'; SET otherreplace = REPLACE (otherreplace,'cust_ids',cust_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by credit_note_details_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `SalesQuantityInvoiceReport`(IN `sdate` DATE, IN `edate` DATE, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `cat_id` VARCHAR(500) CHARSET utf8, IN `cust_id` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8, IN `org_id` INT(11) UNSIGNED) BEGIN DECLARE ids varchar(500); DECLARE cates_id varchar(500); DECLARE other varchar(500); DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET cates_id = REPLACE(cat_id,' ',''); SET other = REPLACE(all_ids,' ',''); SET SQLText ='select invoices.invoice_number,invoices.id as invoices_id,invoices.salesman_id, invoices.customer_id, invoice_details.id as invoice_details_id, invoice_details.item_id, items.item_code, items.item_name,item_uoms.name as uoms_name, SUM(invoice_details.item_qty) as Total_sales_qty FROM invoices left join invoice_details on invoice_details.invoice_id = invoices.id left join items on items.id = invoice_details.item_id left join item_uoms on item_uoms.id = items.lower_unit_uom_id \r\nleft join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id and items.lower_unit_uom_id != 0 WHERE invoices.invoice_date between "sdatess" and "edatess" '; IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',other); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cat_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(items.item_major_category_id, "cates_idss")'; SET otherreplace = REPLACE (otherreplace,'cates_idss',cates_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.customer_id, "cust_ids")'; SET otherreplace = REPLACE (otherreplace,'cust_ids',cust_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by invoice_details.item_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `TimeSheetReport`(IN `sdate` DATE, IN `edate` DATE, IN `salesman_name` VARCHAR(500) CHARSET utf8, IN `salesman_code` VARCHAR(500) CHARSET utf8, IN `org_id` INT(11) UNSIGNED, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8) BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = ' select trips.id, trips.route_id, trips.salesman_id, trips.trip_start, trips.trip_start_date, trips.trip_start_time, trips.trip_end, trips.trip_end_date, trips.trip_end_time, users.firstname,users.lastname,salesman_infos.user_id,salesman_infos.salesman_code from trips LEFT JOIN users on users.id = trips.salesman_id left join salesman_infos on salesman_infos.user_id = trips.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id where exists (select * from customer_visits where trips.id = customer_visits.trip_id and customer_visits.shop_status = "open" and customer_visits.reason is null and customer_visits.deleted_at is null and customer_visits.organisation_id = org_idss ) and exists (select * from customer_visits where trips.id = customer_visits.trip_id and exists (select * from customer_activities where customer_visits.id = customer_activities.customer_visit_id and customer_activities.id is not null and customer_activities.deleted_at is null) and customer_visits.deleted_at is null and customer_visits.organisation_id = org_idss) and trips.deleted_at is null '; SET SQLText = REPLACE (SQLText,'org_idss',org_id); IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND date(trips.trip_start_date) between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' and trips.organisation_id = org_id'; SET otherreplace = REPLACE (otherreplace,'org_id',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(trips.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_name != '' THEN SET SQLText = CONCAT(SQLText,salesman_name); END IF; IF salesman_code != '' THEN SET SQLText = CONCAT(SQLText,salesman_code); END IF; SET otherreplace = ' Group by trips.id order by trips.id desc'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `TimeSheetReportCustomerActivity`(IN `cust_visit_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select * from customer_activities where customer_visit_id = visit_idss'; SET SQLText = REPLACE (SQLText,'visit_idss',cust_visit_id); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `TimeSheetReportCustomerVisit`(IN `trip_id` INT(11) UNSIGNED, IN `org_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select customer_visits.*,customer_infos.customer_code,concat(users.firstname," ",users.lastname) as customer_name, users.firstname,users.lastname from customer_visits LEFT JOIN users on users.id = customer_visits.customer_id LEFT JOIN customer_infos on customer_infos.user_id = users.id where shop_status = "open" and reason is null'; IF trip_id > 0 THEN SET otherreplace = ' and customer_visits.trip_id = trip_idss'; SET otherreplace = REPLACE (otherreplace,'trip_idss',trip_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' and exists (select * from customer_activities where customer_visits.id = customer_activities.customer_visit_id and id is not null and customer_activities.deleted_at is null) and customer_visits.deleted_at is null'; SET SQLText = CONCAT(SQLText,otherreplace); IF org_id > 0 THEN SET otherreplace = ' and customer_visits.organisation_id = ord_idss'; SET otherreplace = REPLACE (otherreplace,'ord_idss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' order by customer_visits.id desc'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `activeOutlets_asm`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `salesman_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesmanSupervisor.firstname AS supervisor,channels.name AS channel,regions.region_name AS region,customer_visits.total_task AS total_tasks_planned,SUM(customer_visits.completed_task) AS no_of_tasks_completed,COUNT(DISTINCT customer_visits.id) as total_visits from salesman_infos inner join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor inner join users as salesman on salesman.id = salesman_infos.user_id inner join users as salesmanSupervisor on salesmanSupervisor.id = salesman_infos.salesman_supervisor left join customer_routes on customer_routes.route_id = salesman_infos.route_id inner join customer_infos on customer_infos.id = customer_routes.customer_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_categories on customer_categories.id = customer_infos.customer_category_id inner join channels on channels.id = customer_infos.channel_id inner join regions on regions.id = customer_infos.region_id where customer_visits.shop_status = "open" and customer_visits.reason is null'; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = org_idss'; SET otherreplace = REPLACE (otherreplace,'org_idss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_id != '' THEN SET otherreplace = ' AND customer_visits.salesman_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,salesman_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.customer_id, customer_visits.date, merchandiser'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `activeOutlets_asm_trends`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select customer_visits.date as date, count(DISTINCT customer_visits.customer_id) as value from salesman_infos inner join customer_visits on salesman_infos.user_id = customer_visits.salesman_id where customer_visits.shop_status = "open" and customer_visits.reason is null '; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND customer_visits.salesman_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.date order by customer_visits.added_on asc'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `activeOutlets_channels`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.date) as date,customerInfo.firstname AS customer,customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesman_infos.salesman_supervisor AS supervisor,channels.name AS channel,regions.region_name AS region,customer_visits.total_task AS total_tasks_planned,SUM(customer_visits.completed_task) AS no_of_tasks_completed,COUNT(DISTINCT customer_visits.id) as total_visits from channels inner join customer_infos on customer_infos.channel_id = channels.id inner join customer_routes on customer_routes.customer_id = customer_infos.id inner join salesman_infos on salesman_infos.route_id = customer_routes.route_id inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_categories on customer_categories.id = customer_infos.customer_category_id inner join regions on regions.id = customer_infos.region_id where customer_visits.shop_status = "open" and customer_visits.completed_task != 0 and customer_visits.reason is null '; IF org_id > 0 THEN SET otherreplace = ' AND channels.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.added_on between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.added_on, channel'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `activeOutlets_else_part`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesmanSupervisor.firstname AS supervisor,channels.name AS channel,regions.region_name AS region,customer_visits.total_task AS total_tasks_planned,SUM(customer_visits.completed_task) AS no_of_tasks_completed,COUNT(DISTINCT customer_visits.id) as total_visits from salesman_infos inner join users as salesman on salesman.id = salesman_infos.user_id inner join users as salesmanSupervisor on salesmanSupervisor.id = salesman_infos.salesman_supervisor inner join customer_routes on customer_routes.route_id = salesman_infos.route_id inner join customer_infos on customer_infos.id = customer_routes.customer_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_lobs on customer_lobs.customer_info_id = customer_infos.id inner join customer_categories on customer_categories.id = customer_lobs.customer_category_id inner join channels on channels.id = customer_lobs.channel_id inner join regions on regions.id = customer_lobs.region_id where customer_visits.shop_status = "open" and customer_visits.reason is null'; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND customer_visits.salesman_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.customer_id, customer_visits.date, merchandiser'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `activeOutlets_nsm`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `salesman_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesmanSupervisor.firstname AS supervisor,channels.name AS channel,regions.region_name AS region,customer_visits.total_task AS total_tasks_planned,SUM(customer_visits.completed_task) AS no_of_tasks_completed,COUNT(DISTINCT customer_visits.id) as total_visits from salesman_infos inner join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor inner join users as salesman on salesman.id = salesman_infos.user_id inner join users as salesmanSupervisor on salesmanSupervisor.id = salesman_infos.salesman_supervisor left join customer_routes on customer_routes.route_id = salesman_infos.route_id inner join customer_infos on customer_infos.id = customer_routes.customer_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_categories on customer_categories.id = customer_infos.customer_category_id inner join channels on channels.id = customer_infos.channel_id inner join regions on regions.id = customer_infos.region_id where customer_visits.shop_status = "open" and customer_visits.reason is null'; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = org_idss'; SET otherreplace = REPLACE (otherreplace,'org_idss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_id != '' THEN SET otherreplace = ' AND customer_visits.salesman_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,salesman_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.customer_id, customer_visits.date, merchandiser'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `activeOutlets_regions`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesman_infos.salesman_supervisor AS supervisor,channels.name AS channel,regions.region_name AS region,customer_visits.total_task AS total_tasks_planned,SUM(customer_visits.completed_task) AS no_of_tasks_completed,COUNT(DISTINCT customer_visits.id) as total_visits from regions inner join customer_infos on customer_infos.region_id = regions.id inner join customer_routes on customer_routes.customer_id = customer_infos.id inner join salesman_infos on salesman_infos.route_id = customer_routes.route_id inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_categories on customer_categories.id = customer_infos.customer_category_id inner join channels on channels.id = customer_infos.channel_id where customer_visits.shop_status = "open" and customer_visits.completed_task != 0 and customer_visits.reason is null'; IF org_id > 0 THEN SET otherreplace = ' AND regions.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.added_on between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.added_on, region'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `activeOutlets_supervisor`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `salesman_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.date) as date,\r\n customerInfo.firstname AS customer,\r\n customer_infos.customer_code AS customerCode,\r\n customer_categories.customer_category_name AS category,\r\n salesman.firstname AS merchandiser,\r\n salesman_infos.salesman_supervisor AS supervisor,\r\n channels.name AS channel,\r\n regions.region_name AS region,\r\n customer_visits.total_task AS total_tasks_planned,\r\n SUM(customer_visits.completed_task) AS no_of_tasks_completed,\r\n COUNT(DISTINCT customer_visits.id) as total_visits from salesman_infos inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_routes on customer_routes.route_id = salesman_infos.route_id inner join customer_infos on customer_infos.id = customer_routes.customer_id inner join customer_visits on salesman_infos.user_id = customer_visits.salesman_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_lobs on customer_lobs.customer_info_id = customer_infos.id inner join customer_categories on customer_categories.id = customer_lobs.customer_category_id inner join channels on channels.id = customer_lobs.channel_id inner join regions on regions.id = customer_lobs.region_id where customer_visits.shop_status = "open" and customer_visits.reason is null'; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = org_idss'; SET otherreplace = REPLACE (otherreplace,'org_idss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,salesman_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.customer_id, customer_visits.date, merchandiser'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `coverage_sp_channel_three`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8mb4) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select customer_visits.added_on as date, count(customer_visits.id) as value from channels inner join customer_infos ON customer_infos.channel_id = channels.id inner join customer_lobs ON customer_lobs.channel_id = channels.id inner join customer_visits ON customer_visits.customer_id = customer_infos.user_id where customer_infos.is_lob = 1 AND customer_visits.shop_status = "open" AND customer_visits.reason is null'; IF org_id > 0 THEN SET otherreplace = ' AND channels.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.date order by customer_visits.added_on asc'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `coverage_sp_coverage_listing_region`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesman_infos.salesman_supervisor AS supervisor,channels.name AS channel,regions.region_name AS region,customer_visits.total_task AS total_tasks_planned,SUM(customer_visits.completed_task) AS no_of_tasks_completed,COUNT(DISTINCT customer_visits.id) as total_visits from regions inner join customer_infos on customer_infos.region_id = regions.id inner join customer_routes on customer_routes.customer_id = customer_infos.id inner join salesman_infos on salesman_infos.route_id = customer_routes.route_id inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_categories on customer_categories.id = customer_infos.customer_category_id inner join channels on channels.id = customer_infos.channel_id where customer_visits.shop_status = "open" and customer_visits.completed_task != 0 and customer_visits.reason is null'; IF org_id > 0 THEN SET otherreplace = ' AND regions.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.added_on, region'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `coverage_sp_listing_channel`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesman_infos.salesman_supervisor AS supervisor,channels.name AS channel,regions.region_name AS region,customer_visits.total_task AS total_tasks_planned,SUM(customer_visits.completed_task) AS no_of_tasks_completed,COUNT(DISTINCT customer_visits.id) as total_visits from channels inner join customer_infos on customer_infos.channel_id = channels.id inner join customer_routes on customer_routes.customer_id = customer_infos.id inner join salesman_infos on salesman_infos.route_id = customer_routes.route_id inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_categories on customer_categories.id = customer_infos.customer_category_id inner join regions on regions.id = customer_infos.region_id where customer_visits.shop_status = "open" and customer_visits.completed_task != 0 and customer_visits.reason is null'; IF org_id > 0 THEN SET otherreplace = ' AND channels.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.added_on, channel'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `coverage_sp_listing_data_last`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesmanSupervisor.firstname AS supervisor,channels.name AS channel,regions.region_name AS region,customer_visits.total_task AS total_tasks_planned,SUM(customer_visits.completed_task) AS no_of_tasks_completed,COUNT(DISTINCT customer_visits.id) as total_visits from salesman_infos inner join users as salesman on salesman.id = salesman_infos.user_id inner join users as salesmanSupervisor on salesmanSupervisor.id = salesman_infos.salesman_supervisor left join customer_routes on customer_routes.route_id = salesman_infos.route_id inner join customer_infos on customer_infos.id = customer_routes.customer_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_lobs on customer_lobs.customer_info_id = customer_infos.id inner join customer_categories on customer_categories.id = customer_lobs.customer_category_id inner join channels on channels.id = customer_lobs.channel_id inner join regions on regions.id = customer_lobs.region_id where customer_visits.shop_status = "open" and customer_visits.reason is null'; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND customer_visits.salesman_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.customer_id, customer_visits.date'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `coverage_sp_listing_data_nsm`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `nsm_id` LONGTEXT CHARSET utf8, IN `salesman_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,\r\n customerInfo.firstname AS customer,\r\n customer_infos.customer_code AS customerCode,\r\n customer_categories.customer_category_name AS category,\r\n salesman.firstname AS merchandiser,\r\n salesmanSupervisor.firstname AS supervisor,\r\n channels.name AS channel,\r\n regions.region_name AS region,\r\n customer_visits.total_task AS total_tasks_planned,\r\n SUM(customer_visits.completed_task) AS no_of_tasks_completed,\r\n COUNT(DISTINCT customer_visits.id) as total_visits from salesman_infos inner join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor inner join users as salesman on salesman.id = salesman_infos.user_id inner join users as salesmanSupervisor on salesmanSupervisor.id = salesman_infos.salesman_supervisor left join customer_routes on customer_routes.route_id = salesman_infos.route_id inner join customer_infos on customer_infos.id = customer_routes.customer_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_categories on customer_categories.id = customer_infos.customer_category_id inner join channels on channels.id = customer_infos.channel_id inner join regions on regions.id = customer_infos.region_id where customer_visits.shop_status = "open" '; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = org_idss'; SET otherreplace = REPLACE (otherreplace,'org_idss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_id != '' THEN SET otherreplace = ' AND customer_visits.salesman_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,salesman_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.customer_id, customer_visits.date, merchandiser'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `coverage_sp_listing_data_one`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select DISTINCT DATE(customer_visits.date) as date, customerInfo.firstname AS customer, customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesman_infos.salesman_supervisor AS supervisor,channels.name AS channel,regions.region_name AS region,customer_visits.total_task AS total_tasks_planned,SUM(customer_visits.completed_task) AS no_of_tasks_completed,COUNT(DISTINCT customer_visits.id) as total_visits from salesman_infos inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_routes on customer_routes.route_id = salesman_infos.route_id \r\ninner join customer_infos on customer_infos.id = customer_routes.customer_id inner join customer_visits on salesman_infos.user_id = customer_visits.salesman_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_lobs on customer_lobs.customer_info_id = customer_infos.id \r\ninner join customer_categories on customer_categories.id = customer_lobs.customer_category_id inner join channels on channels.id = customer_lobs.channel_id inner join regions on regions.id = customer_lobs.region_id where customer_visits.shop_status = "open" and customer_visits.reason is null '; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.customer_id, customer_visits.date, merchandiser'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `coverage_sp_one`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sup_id` LONGTEXT CHARSET utf8, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); DECLARE cust_idss LONGTEXT; SET cust_idss = cust_id; SET SQLText = 'select customer_visits.date as date, count(DISTINCT customer_visits.customer_id) as value from salesman_infos inner join customer_visits on salesman_infos.user_id = customer_visits.salesman_id where customer_visits.shop_status = "open" and customer_visits.reason is null '; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ( sup_idss)'; SET otherreplace = REPLACE (otherreplace,'sup_idss',sup_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.date order by customer_visits.added_on asc'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `coverage_sp_two`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select customer_visits.date as date, count(DISTINCT customer_visits.customer_id) as value from salesman_infos inner join customer_visits on salesman_infos.user_id = customer_visits.salesman_id where customer_visits.shop_status = "open" and customer_visits.reason is null '; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND customer_visits.salesman_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.date order by customer_visits.added_on asc'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `dashboardTotalCollection`(IN `sdate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select SUM(invoice_amount) as invoiceAmt from collections left join salesman_infos on salesman_infos.user_id = collections.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor'; IF org_id > 0 THEN SET otherreplace = ' WHERE collections.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sdate IS NOT NULL THEN SET otherreplace = ' AND date(collections.created_at) = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND collections.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `dashboardTotalCollectionThirtyDays`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select SUM(invoice_amount) as invoiceAmt from collections left join salesman_infos on salesman_infos.user_id = collections.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor'; IF org_id > 0 THEN SET otherreplace = ' WHERE collections.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND date(collections.created_at) between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND collections.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `dashboardTotalSales`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select invoices.id as invoices_id,invoices.salesman_id, invoices.customer_id, SUM(invoices.total_gross) as Total_sales FROM invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor LEFT JOIN users on users.id = invoices.salesman_id '; IF org_id > 0 THEN SET otherreplace = ' WHERE invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sdate IS NOT NULL THEN SET otherreplace = ' AND invoices.invoice_date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `dashboardTotalSalesThirtyDays`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select invoices.id as invoices_id,invoices.salesman_id, invoices.customer_id, SUM(invoices.total_gross) as Total_sales FROM invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor LEFT JOIN users on users.id = invoices.salesman_id '; IF org_id > 0 THEN SET otherreplace = ' WHERE invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND date(invoices.invoice_date) between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `dashboardTotalSalesUnit`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select invoices.id as invoices_id FROM invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor LEFT JOIN users on users.id = invoices.salesman_id '; IF org_id > 0 THEN SET otherreplace = ' WHERE invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sdate IS NOT NULL THEN SET otherreplace = ' AND invoices.invoice_date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `dashboardTotalSalesUnitDetails`(IN `invoice_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select SUM(lower_unit_qty) as lowerUnitQty FROM invoice_details'; IF invoice_id != '' THEN SET otherreplace = ' WHERE invoice_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,invoice_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `dashboardTotalSalesUnitThirtyDays`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select SUM(lower_unit_qty) as lowerUnitQty FROM invoices left join invoice_details on invoice_details.invoice_id = invoices.id left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor LEFT JOIN users on users.id = invoices.salesman_id '; IF org_id > 0 THEN SET otherreplace = ' WHERE invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND date(invoices.invoice_date) between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `dropSizeCustomer`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select sum(total_gross) as totalCustInvoice, count(DISTINCT customer_id) as total_customers from invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor'; IF org_id > 0 THEN SET otherreplace = ' WHERE invoices.organisation_id = orgidss AND customer_id IS NOT NULL'; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND date(invoices.invoice_date) between "sdatess" and "edatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `dropSizeCustomerDayWise`(IN `sdate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select sum(total_gross) as totalCustInvoice, count(DISTINCT customer_id) as total_customers from invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor'; IF org_id > 0 THEN SET otherreplace = ' WHERE invoices.organisation_id = orgidss AND customer_id IS NOT NULL'; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL) THEN SET otherreplace = ' AND date(invoices.invoice_date) = "sdatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `dropSizeRate`(IN `sdate` DATE, IN `org_id` INT(11), IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'SELECT SUM(grand_total) as total_sale, count(invoices.id) as total_invoices from invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor WHERE invoice_date = "sdatess" '; SET SQLText = REPLACE (SQLText,'sdatess',sdate); IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `dropSizeTotalCustomerItems`(IN `customer_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select count(invoice_details.id) as totalItems from invoice_details LEFT JOIN invoices on invoices.id = invoice_details.invoice_id '; IF customer_id > 0 THEN SET otherreplace = ' WHERE invoices.customer_id = custidss '; SET otherreplace = REPLACE (otherreplace,'custidss',customer_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `execution_sp_listing`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8mb4) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesman_infos.salesman_supervisor AS supervisor,customer_infos.customer_code AS customerCode,channels.name AS channel,regions.region_name AS region,customer_visits.start_time AS startTime,customer_visits.end_time AS endTime,customer_visits.total_task as totalTask,customer_visits.completed_task as completedTask,customer_visits.visit_total_time as timeSpent,customer_visits.latitude AS latitude,customer_visits.longitude AS longitude from channels inner join customer_infos on customer_infos.channel_id = channels.id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_routes on customer_routes.route_id = customer_infos.route_id inner join salesman_infos on salesman_infos.route_id = customer_routes.route_id inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_visits on customer_visits.customer_id = customer_infos.user_id inner join regions on regions.id = customer_infos.region_id inner join customer_categories on customer_categories.id = customer_infos.customer_category_id where customer_visits.shop_status = "open" and customer_visits.reason is null AND customer_visits.completed_task != 0 and customer_infos.status = 1'; IF org_id > 0 THEN SET otherreplace = ' AND customer_visits.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL AND edate != '0000-00-00' AND sdate != '0000-00-00') THEN SET otherreplace = ' AND customer_visits.added_on between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `execution_sp_listing_else_part`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,\r\n customerInfo.firstname AS customer,\r\n customer_categories.customer_category_name AS category,\r\n salesman.firstname AS merchandiser,\r\n customer_infos.customer_code AS customerCode,\r\n salesmanSupervisor.firstname AS supervisor,\r\n channels.name AS channel,\r\n regions.region_name AS region,\r\n customer_visits.start_time AS startTime,\r\n customer_visits.end_time AS endTime,\r\n customer_visits.total_task as totalTask,\r\n customer_visits.completed_task as completedTask,\r\n customer_visits.visit_total_time as timeSpent,\r\n customer_visits.latitude AS latitude,\r\n customer_visits.longitude AS longitude from salesman_infos inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_routes on customer_routes.route_id = salesman_infos.route_id inner join customer_infos on customer_infos.id = customer_routes.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join users as salesmanSupervisor on salesmanSupervisor.id = salesman_infos.salesman_supervisor inner join customer_visits on customer_visits.customer_id = customer_infos.user_id inner join customer_lobs on customer_lobs.customer_info_id = customer_infos.id inner join customer_categories on customer_categories.id = customer_lobs.customer_category_id inner join channels on channels.id = customer_lobs.channel_id inner join regions on regions.id = customer_lobs.region_id where customer_visits.shop_status = "open" and customer_visits.completed_task != 0 and customer_visits.reason is null '; IF org_id > 0 THEN SET otherreplace = ' AND customer_visits.organisation_id = org_idss'; SET otherreplace = REPLACE (otherreplace,'org_idss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.added_on between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `execution_sp_listing_region`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,customer_infos.customer_code AS customerCode,salesman_infos.salesman_supervisor AS supervisor,channels.name AS channel,regions.region_name AS region,customer_visits.start_time AS startTime,customer_visits.end_time AS endTime,customer_visits.total_task as totalTask,customer_visits.completed_task as completedTask,customer_visits.visit_total_time as timeSpent,customer_visits.latitude AS latitude,customer_visits.longitude AS longitude from regions inner join customer_infos on customer_infos.region_id = regions.id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_routes on customer_routes.route_id = customer_infos.route_id inner join salesman_infos on salesman_infos.route_id = customer_routes.route_id inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_visits on customer_visits.customer_id = customer_infos.user_id inner join channels on channels.id = customer_infos.channel_id inner join customer_categories on customer_categories.id = customer_infos.customer_category_id where customer_visits.shop_status = "open" and customer_visits.completed_task != 0 and customer_infos.status = 1 and customer_visits.reason is null and customer_visits.deleted_at is null'; IF org_id > 0 THEN SET otherreplace = ' AND customer_visits.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL AND edate != '0000-00-00' AND sdate != '0000-00-00') THEN SET otherreplace = ' AND customer_visits.added_on between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `getCategoryAndTotalSale`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8, IN `region_id` INT(11) UNSIGNED, IN `cat_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'SELECT SUM(invoice_details.item_grand_total) as prod_sale from customer_infos LEFT JOIN customer_lobs on customer_lobs.customer_info_id = customer_infos.id LEFT JOIN invoices on invoices.customer_id = customer_infos.user_id left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor LEFT JOIN invoice_details on invoice_details.invoice_id = invoices.id LEFT JOIN items on items.id = invoice_details.item_id LEFT JOIN item_major_categories on item_major_categories.id = items.item_major_category_id where customer_infos.region_id IS NOT NULL AND item_major_categories.name IS NOT NULL '; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND invoices.invoice_date between "sdatess" and "edatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_id > 0 THEN SET otherreplace = ' AND customer_infos.region_id = regionidss '; SET otherreplace = REPLACE (otherreplace,'regionidss',region_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cat_id > 0 THEN SET otherreplace = ' AND item_major_categories.id = catidss '; SET otherreplace = REPLACE (otherreplace,'catidss',cat_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' GROUP BY items.item_major_category_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `getCreditNoteReport`(IN `sdate` DATE, IN `edate` DATE, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `cat_id` VARCHAR(500) CHARSET utf8, IN `cust_id` VARCHAR(500) CHARSET utf8, IN `org_id` INT(11) UNSIGNED, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8) BEGIN DECLARE ids varchar(500); DECLARE cates_id varchar(500); DECLARE other varchar(500); DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET cates_id = REPLACE(cat_id,' ',''); SET other = REPLACE(all_ids,' ',''); SET SQLText ='select credit_notes.id as credit_notes_id, credit_notes.salesman_id, credit_notes.customer_id, credit_note_details.id as credit_note_details_id, credit_note_details.item_id, items.item_code, items.item_name, SUM(credit_note_details.item_gross) as Total_creditnote from credit_notes left join credit_note_details on credit_note_details.credit_note_id = credit_notes.id left join items on items.id = credit_note_details.item_id left join salesman_infos on salesman_infos.user_id = credit_notes.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id WHERE credit_notes.credit_note_date between "sdatess" and "edatess" '; IF org_id > 0 THEN SET otherreplace = ' AND items.organisation_id = org_idss '; SET otherreplace = REPLACE (otherreplace,'org_idss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sdate IS NOT NULL AND edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(credit_notes.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',other); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cat_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(items.item_major_category_id, ''cates_idss'')'; SET otherreplace = REPLACE (otherreplace,'cates_idss',cates_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(credit_notes.customer_id, "cust_ids")'; SET otherreplace = REPLACE (otherreplace,'cust_ids',cust_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by credit_note_details.item_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `getCustomerBalanceSheet`(IN `sdate` DATE, IN `edate` DATE, IN `customer_id` VARCHAR(500) CHARSET utf8, IN `org_id` INT(11) UNSIGNED, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8) BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='(select DATE_FORMAT(debit_note_date,"%d/%m/%Y") as c_date,(CASE WHEN is_debit_note=1 THEN "Debit Note" WHEN is_debit_note=0 THEN (select t2.item_name from debit_note_listingfee_shelfrent_rebatediscount_details t2 where t2.debit_note_id = debit_notes.id) END ) as transaction ,debit_note_number as detail, "0.00" as amount, grand_total as payment, 4 as status, debit_notes.created_at,(CASE WHEN debit_notes.lob_id=1 THEN "SPICES DIVISION" WHEN debit_notes.lob_id=2 THEN "RTC" WHEN debit_notes.lob_id=3 THEN "RICE DIVISION" END) as debitnote_lob_names,grand_total as balance from debit_notes left join salesman_infos on salesman_infos.user_id = debit_notes.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id where debit_notes.debit_note_date between "sdate" and "edate" and debit_notes.deleted_at is null and debit_notes.organisation_id = org_id '; IF customer_id > 0 THEN SET otherreplace = ' AND debit_notes.customer_id = customer_idsss '; SET otherreplace = REPLACE (otherreplace,'customer_idsss',customer_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(debit_notes.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND salesman_lobs.lob_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,div_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND salesman_infos.region_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,region_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND salesman_infos.route_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,route_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,supervisor_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,salesman_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' order by debit_note_date asc)'; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText, ' union (select DATE_FORMAT(invoice_date,"%d/%m/%Y") as c_date,"Invoice" as transaction,CONCAT(invoice_number," - due on ",DATE_FORMAT(invoice_due_date,"%d/%m/%y")) as detail,grand_total as amount,"0.00" as payment,1 as status, invoices.created_at,(CASE WHEN invoices.lob_id=1 THEN "SPICES DIVISION" WHEN invoices.lob_id=2 THEN "RTC" WHEN invoices.lob_id=3 THEN "RICE DIVISION" END) as debitnote_lob_names,grand_total as balance from invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id where invoice_date between "sdate" and "edate" and invoices.deleted_at is null and invoices.organisation_id = org_id '); IF customer_id > 0 THEN SET otherreplace = ' AND invoices.customer_id = customer_idsss'; SET otherreplace = REPLACE (otherreplace,'customer_idsss',customer_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND salesman_lobs.lob_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,div_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND salesman_infos.region_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,region_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND salesman_infos.route_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,route_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,supervisor_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,salesman_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' order by invoices.created_at asc)'; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText, ' union (select DATE_FORMAT(collection_details.created_at,"%d/%m/%Y") as\r\nc_date,"Collection" as transaction, CONCAT(" For payment of ",collections.collection_number),\r\n"0.00" as amount,amount as payment,2 as\r\nstatus,collection_details.created_at,(CASE WHEN collection_details.lob_id=1 THEN "SPICES DIVISION" WHEN collection_details.lob_id=2 THEN "RTC" WHEN collection_details.lob_id=3 THEN "RICE DIVISION" END ) as debitnote_lob_names, amount as balance from collection_details left join collections on collections.id = collection_details.collection_id \r\nleft join salesman_infos on salesman_infos.user_id = collections.salesman_id \r\nleft join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id \r\nwhere collection_details.created_at between "sdate" and "edate" and collection_details.deleted_at is null'); IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(collections.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND salesman_lobs.lob_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,div_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND salesman_infos.region_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,region_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND salesman_infos.route_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,route_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,supervisor_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,salesman_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF customer_id > 0 THEN SET otherreplace = ' AND collection_details.customer_id = customer_idsss'; SET otherreplace = REPLACE (otherreplace,'customer_idsss',customer_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' order by collection_details.created_at asc)'; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText =CONCAT(SQLText, ' union (select DATE_FORMAT(credit_note_date,"%d/%m/%Y") as c_date,"Credit Note" as transaction,credit_note_number as detail, "0.00" as amount, grand_total as payment,3 as status, credit_notes.created_at,(CASE WHEN credit_notes.lob_id=1 THEN "SPICES DIVISION" WHEN credit_notes.lob_id=2 THEN "RTC" WHEN credit_notes.lob_id=3 THEN "RICE DIVISION" END) as debitnote_lob_names, grand_total as balance from credit_notes left join salesman_infos on salesman_infos.user_id = credit_notes.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id where credit_notes.credit_note_date between "sdate" and "edate" and credit_notes.deleted_at is null and credit_notes.organisation_id = org_id '); IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(credit_notes.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND salesman_lobs.lob_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,div_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND salesman_infos.region_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,region_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND salesman_infos.route_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,route_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,supervisor_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,salesman_this); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF customer_id > 0 THEN SET otherreplace = ' AND credit_notes.customer_id = customer_idsss'; SET otherreplace = REPLACE (otherreplace,'customer_idsss',customer_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' order by credit_notes.created_at asc) order by created_at asc'; SET SQLText = CONCAT(SQLText,otherreplace); IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdate',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edate',edate); END IF; IF org_id > 0 THEN SET SQLText = REPLACE (SQLText,'org_id',org_id); END IF; IF org_id > 0 THEN SET SQLText = REPLACE (SQLText,'org_id',org_id); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `getCustomerDropDownList`(IN `_type` INT(20), IN `_search_value` VARCHAR(250), IN `org_id` INT(20)) BEGIN SELECT u.id, CONCAT(u.firstname, u.lastname) AS name, c.customer_code,c.payment_term_id, c.is_lob,c.user_id,u.organisation_id FROM users AS u INNER JOIN customer_infos AS c ON c.user_id = u.id WHERE u.usertype = _type AND u.organisation_id = org_id AND ( u.firstname LIKE CONCAT('%', _search_value , '%') || u.lastname LIKE CONCAT('%', _search_value , '%') || c.customer_code LIKE CONCAT('%', _search_value , '%') ); END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `getItemDropDownList`(IN `_search_value` VARCHAR(250)) BEGIN SELECT i.id,i.uuid,i.item_name,i.item_code,i.lower_unit_item_upc,i.stock_keeping_unit,i.lower_unit_item_price FROM `items` as i WHERE ( i.item_name LIKE CONCAT('%', _search_value , '%') || i.item_code LIKE CONCAT('%', _search_value , '%') ); END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `getRouteInvoice`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'SELECT id as route_id, route_name from routes WHERE deleted_at IS NULL'; IF org_id > 0 THEN SET otherreplace = ' AND organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `getRouteVisit`(IN `sdate` DATE, IN `edate` DATE, IN `div_id` VARCHAR(500) CHARSET utf8, IN `org_id` INT(11) UNSIGNED, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8) NO SQL BEGIN DECLARE divid varchar(500); DECLARE SQLText varchar(8000); DECLARE otherreplace varchar(500); SET divid = REPLACE(div_id,' ',''); SET SQLText ='SELECT SUM(CASE WHEN customer_visits.journey_plan_id > 0 THEN 1 ELSE 0 END) as total_journey,SUM(CASE WHEN customer_visits.is_sequnece = "1" THEN 1 ELSE 0 END) as planed_journey,SUM(CASE WHEN customer_visits.is_sequnece = "0" THEN 1 ELSE 0 END) as un_planed_journey,customer_visits.id, customer_visits.customer_id, customer_visits.route_id, customer_visits.journey_plan_id,customer_visits.salesman_id, customer_visits.latitude,customer_visits.longitude,customer_visits.start_time,customer_visits.end_time,customer_visits.is_sequnece,customer_visits.date,customer_visits.created_at, salesman_infos.route_id as salesman_route,salesman_infos.salesman_code,CONCAT(users.firstname," ",users.lastname) as salesman_name,customer_visits.salesman_id FROM customer_visits LEFT JOIN salesman_infos ON salesman_infos.user_id = customer_visits.salesman_id LEFT JOIN salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id LEFT JOIN users on users.id = salesman_infos.user_id WHERE '; IF (sdate = edate) THEN SET otherreplace = ' customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' customer_visits.date between "sdatess" and ''edatess'' '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(customer_visits.salesman_id, "dividss")'; SET otherreplace = REPLACE (otherreplace,'dividss',divid); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND customer_visits.organisation_id = org_idsss'; SET otherreplace = REPLACE (otherreplace,'org_idsss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.salesman_id, customer_visits.date,customer_visits.customer_id order by customer_visits.id desc,customer_visits.id DESC'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `getRouteWiseInvoiceIds`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `route_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select invoices.id as invoices_id, SUM(invoices.total_gross) as Total_sales_qty FROM invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor WHERE invoices.invoice_date between "sdatess" and "edatess" '; IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_id > 0 THEN SET otherreplace = ' AND invoices.route_id = routeidss '; SET otherreplace = REPLACE (otherreplace,'routeidss',route_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `getSalesAnalysisReport`(IN `sdate` DATE, IN `edate` DATE, IN `all_ids` LONGTEXT CHARSET utf8, IN `cat_id` LONGTEXT CHARSET utf8, IN `cust_id` LONGTEXT CHARSET utf8, IN `div_this` LONGTEXT CHARSET utf8, IN `region_this` LONGTEXT CHARSET utf8, IN `route_this` LONGTEXT CHARSET utf8, IN `supervisor_this` LONGTEXT CHARSET utf8, IN `salesman_this` LONGTEXT CHARSET utf8, IN `item_code` LONGTEXT CHARSET utf8, IN `item_name` LONGTEXT, IN `net_sale` LONGTEXT, IN `total_sale` LONGTEXT, IN `org_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE ids varchar(500); DECLARE cates_id varchar(500); DECLARE other varchar(500); DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET cates_id = REPLACE(cat_id,' ',''); SET other = REPLACE(all_ids,' ',''); SET SQLText ='select invoices.id as invoices_id,invoices.salesman_id, invoices.customer_id, invoice_details.id as invoice_details_id, invoice_details.item_id, items.item_code, items.item_name, SUM(invoice_details.item_grand_total) as Total_invoice_sales, SUM(invoice_details.item_net) as Total_invoice_net FROM invoices left join invoice_details on invoice_details.invoice_id = invoices.id left join items on items.id = invoice_details.item_id left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id WHERE invoices.invoice_date between "sdatess" and "edatess" AND items.item_name IS NOT NULL'; IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',other); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cat_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(items.item_major_category_id, "cates_idss")'; SET otherreplace = REPLACE (otherreplace,'cates_idss',cates_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.customer_id, "cust_ids")'; SET otherreplace = REPLACE (otherreplace,'cust_ids',cust_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF item_code != '' THEN SET otherreplace = ' AND items.item_code = "item_codess" '; SET otherreplace = REPLACE (otherreplace,'item_codess',item_code); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF item_name != '' THEN SET otherreplace = ' AND items.item_name LIKE "%item_namess%" '; SET otherreplace = REPLACE (otherreplace,'item_namess',item_name); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by invoice_details.item_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `getTEST`(IN `sdate` DATE, IN `edate` DATE, IN `route_id` VARCHAR(500) CHARSET utf8, IN `all_ids` VARCHAR(500) CHARSET utf8) BEGIN DECLARE ids varchar(500); DECLARE cates_id varchar(500); DECLARE other varchar(500); DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET ids = REPLACE(route_id,' ',''); SET SQLText ='select invoices.id as invoices_id,invoices.salesman_id, invoices.customer_id, invoice_details.id as invoice_details_id, invoice_details.item_id, items.item_code, items.item_name, SUM(invoice_details.item_grand_total) as Total_invoice_sales, SUM(invoice_details.item_net) as Total_invoice_net FROM invoices left join invoice_details on invoice_details.invoice_id = invoices.id left join items on items.id = invoice_details.item_id WHERE invoices.invoice_date between "sdatess" and "edatess" '; IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',other); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.route_id, "routeidss")'; SET otherreplace = REPLACE (otherreplace,'routeidss',ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by invoice_details.item_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `getUniqueSalesmanIdFrom_salesman_unloads_tbl`() SELECT DISTINCT(salesman_id) FROM salesman_unloads$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `loadsheetreports`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select load_requests.id, load_requests.route_id, load_requests.salesman_id, load_requests.load_number, load_requests.load_type, load_requests.load_date, load_requests.status, routes.route_code, routes.route_name,concat(users.firstname,"",users.lastname) as salesman_name, users.firstname, users.lastname, salesman_infos.salesman_code, salesman_infos.user_id, load_request_details.load_request_id,load_request_details.item_id ,load_request_details.item_uom_id ,load_request_details.qty,load_request_details.requested_qty,items.item_name, items.item_code ,item_uoms.name as item_uom_name, item_uoms.id as item_uoms_id, item_uoms.code as item_uoms_code FROM load_requests LEFT JOIN routes ON routes.id = load_requests.route_id LEFT JOIN users ON users.id = load_requests.salesman_id left join salesman_infos on salesman_infos.user_id = load_requests.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id LEFT JOIN load_request_details ON load_request_details.load_request_id = load_requests.id LEFT JOIN items ON items.id = load_request_details.item_id LEFT JOIN item_uoms ON item_uoms.id = load_request_details.item_uom_id WHERE load_requests.load_date between "sdatess" and "edatess" AND load_requests.deleted_at is null and load_requests.organisation_id = org_idss'; SET SQLText = REPLACE (SQLText,'sdatess',sdate); SET SQLText = REPLACE (SQLText,'edatess',edate); SET SQLText = REPLACE (SQLText,'org_idss',org_id); IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(load_requests.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' Group By load_requests.id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `loadsheetreportsDetails`(IN `load_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select load_request_details.load_request_id,load_request_details.item_id ,load_request_details.item_uom_id ,load_request_details.qty,load_request_details.requested_qty,items.item_name, items.item_code ,item_uoms.name as item_uom_name, item_uoms.id as item_uoms_id, item_uoms.code as item_uoms_code FROM load_request_details LEFT JOIN items ON items.id = load_request_details.item_id LEFT JOIN item_uoms ON item_uoms.id = load_request_details.item_uom_id where load_request_details.load_request_id = loadrequest_idss'; SET SQLText = REPLACE (SQLText,'loadrequest_idss',load_id); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `mostSoldItems`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select invoices.id,count(invoice_details.item_qty) as totalQty,sum(invoice_details.item_grand_total) as totalValue, invoice_details.item_id,items.item_name, items.item_code from invoice_details LEFT JOIN invoices on invoices.id = invoice_details.invoice_id left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor LEFT JOIN items on items.id = invoice_details.item_id'; IF org_id > 0 THEN SET otherreplace = ' WHERE invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND invoices.invoice_date between "sdatess" and "edatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' GROUP BY invoice_details.item_id ORDER BY totalQty DESC'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `orderReport`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8) BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select orders.id, orders.customer_id, orders.depot_id, orders.order_type_id, orders.salesman_id, orders.order_number, orders.order_date, orders.due_date, orders.delivery_date, orders.payment_term_id, orders.total_qty, orders.total_gross, orders.total_discount_amount, orders.total_net, orders.total_vat, orders.total_excise, orders.grand_total, orders.current_stage, orders.lob_id, concat(custUser.firstname," ",custUser.lastname) AS customer_fullname, concat(salesUser.firstname," ",salesUser.lastname) AS salesman_fullname, customer_infos.user_id,customer_infos.customer_code, salesman_infos.salesman_code, order_types.name as orderTypeName, order_types.description,payment_terms.name as paymentTermsName, payment_terms.number_of_days,lobs.name as lobsname FROM orders LEFT JOIN users as custUser ON custUser.id = orders.customer_id LEFT JOIN customer_infos ON customer_infos.user_id = custUser.id LEFT JOIN users as salesUser ON salesUser.id = orders.salesman_id left join salesman_infos on salesman_infos.user_id = orders.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id LEFT JOIN order_types ON order_types.id = orders.order_type_id LEFT JOIN payment_terms ON payment_terms.id = orders.payment_term_id LEFT JOIN depots ON depots.id = orders.depot_id LEFT JOIN lobs ON lobs.id = orders.lob_id where orders.order_date between "sdatess" and "edatess" AND orders.organisation_id = org_idss AND orders.deleted_at is null'; SET SQLText = REPLACE (SQLText,'sdatess',sdate); SET SQLText = REPLACE (SQLText,'edatess',edate); SET SQLText = REPLACE (SQLText,'org_idss',org_id); IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(orders.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' Group by orders.id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `orderReportDetails`(IN `order_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select order_details.order_id, order_details.item_id, order_details.item_uom_id, order_details.discount_id, order_details.is_free, order_details.is_item_poi, order_details.promotion_id, order_details.item_qty, order_details.item_price, order_details.item_gross , order_details.item_discount_amount , order_details.item_net , order_details.item_vat , order_details.item_excise , order_details.item_grand_total , order_details.delivered_qty , order_details.open_qty , order_details.order_status, items.item_name, items.item_code,item_uoms.name as itemUomName,item_uoms.code as itemUomCode, price_disco_promo_plans.name as priceDiscoPromoName, discount.name as discountName FROM order_details LEFT JOIN items ON items.id = order_details.item_id LEFT JOIN item_uoms ON item_uoms.id = order_details.item_uom_id LEFT JOIN price_disco_promo_plans ON price_disco_promo_plans.id = order_details.promotion_id LEFT JOIN price_disco_promo_plans as discount ON price_disco_promo_plans.id = order_details.discount_id '; IF order_id > 0 THEN SET otherreplace = ' where order_details.order_id = order_idss'; SET otherreplace = REPLACE (otherreplace,'order_idss',order_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by items.item_name'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `product_summary_credit_note_details_details`(IN `credit_note_id` VARCHAR(5000)) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select items.id as item_id, items.item_name, items.item_code,item_uoms.name,item_uoms.code,credit_note_details.item_grand_total, sum(credit_note_details.item_qty), credit_note_details.item_price, credit_note_details.item_gross FROM credit_note_details left join items on items.id = credit_note_details.item_id left join item_uoms on item_uoms.id = items.lower_unit_uom_id '; IF credit_note_id != '' THEN SET otherreplace = ' WHERE credit_note_details.credit_note_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,credit_note_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by credit_note_details.credit_note_id,item_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `product_summary_creditnote`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `customer_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `div_this` LONGTEXT CHARSET utf8, IN `region_this` LONGTEXT CHARSET utf8, IN `route_this` LONGTEXT CHARSET utf8, IN `supervisor_this` LONGTEXT CHARSET utf8, IN `salesman_this` LONGTEXT CHARSET utf8, IN `invoice_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select SUM(credit_notes.grand_total) as total_sale,SUM(credit_notes.total_net) as total_net, GROUP_CONCAT(credit_notes.id) as credit_notes_id, concat(users.firstname," ",users.lastname) as customer_name,customer_infos.customer_code FROM credit_notes left join customer_infos on customer_infos.user_id = credit_notes.customer_id LEFT JOIN users on users.id = customer_infos.user_id'; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' WHERE credit_notes.credit_note_date between "sdatess" and "edatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND credit_notes.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF customer_id != '' THEN SET otherreplace = ' AND credit_notes.customer_id = cust_idss'; SET otherreplace = REPLACE (otherreplace,'cust_idss',customer_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND credit_notes.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF invoice_id != '' THEN SET otherreplace = ' AND credit_notes.invoice_id = invoice_idss'; SET otherreplace = REPLACE (otherreplace,'invoice_idss',invoice_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by credit_notes.customer_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `product_summary_invoice`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `customer_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `div_this` LONGTEXT CHARSET utf8, IN `region_this` LONGTEXT CHARSET utf8, IN `route_this` LONGTEXT CHARSET utf8, IN `supervisor_this` LONGTEXT CHARSET utf8, IN `salesman_this` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select SUM(invoices.grand_total) as total_sale,SUM(invoices.total_net) as total_net, GROUP_CONCAT(invoices.id) as invoices_id, invoices.customer_id as customer_id, concat(users.firstname," ",users.lastname) as customer_name,customer_infos.customer_code FROM invoices left join customer_infos on customer_infos.user_id = invoices.customer_id LEFT JOIN users on users.id = customer_infos.user_id'; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' WHERE invoices.invoice_date between "sdatess" and "edatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF customer_id != '' THEN SET otherreplace = ' AND invoices.customer_id = cust_idss'; SET otherreplace = REPLACE (otherreplace,'cust_idss',customer_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND invoices.salesman_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by invoices.customer_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `product_summary_invoice_details`(IN `invoice_id` VARCHAR(5000)) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select items.id as item_id, items.item_name, items.item_code,item_uoms.name,item_uoms.code,invoice_details.item_grand_total, invoice_details.item_qty, invoice_details.item_price, invoice_details.item_gross FROM invoice_details left join items on items.id = invoice_details.item_id left join item_uoms on item_uoms.id = items.lower_unit_uom_id '; IF invoice_id != '' THEN SET otherreplace = ' WHERE invoice_details.invoice_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,invoice_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `reSalableReport`(IN `sdate` DATE, IN `edate` DATE, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `cat_id` VARCHAR(500) CHARSET utf8, IN `cust_id` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8, IN `org_id` INT(11) UNSIGNED) BEGIN DECLARE ids varchar(500); DECLARE cates_id varchar(500); DECLARE other varchar(500); DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET cates_id = REPLACE(cat_id,' ',''); SET other = REPLACE(all_ids,' ',''); SET SQLText ='select invoices.invoice_number,invoices.id as invoices_id,invoices.salesman_id, invoices.customer_id, invoice_details.id as invoice_details_id, invoice_details.item_id, items.item_code, items.item_name,item_uoms.name as uoms_name, SUM(invoice_details.item_qty) as Total_sales_qty FROM invoices left join invoice_details on invoice_details.invoice_id = invoices.id left join items on items.id = invoice_details.item_id left join item_uoms on item_uoms.id = items.lower_unit_uom_id left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id and items.lower_unit_uom_id != 0 WHERE invoices.invoice_date between "sdatess" and "edatess" AND invoice_details.is_resalable = 1 '; IF sdate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'sdatess',sdate); END IF; IF edate IS NOT NULL THEN SET SQLText = REPLACE (SQLText,'edatess',edate); END IF; IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',other); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cat_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(items.item_major_category_id, "cates_idss")'; SET otherreplace = REPLACE (otherreplace,'cates_idss',cates_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND FIND_IN_SET(invoices.customer_id, "cust_ids")'; SET otherreplace = REPLACE (otherreplace,'cust_ids',cust_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by invoices_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `regionWiseProductSale`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'SELECT invoices.id from regions LEFT JOIN customer_infos on customer_infos.region_id = regions.id LEFT JOIN customer_lobs on customer_lobs.customer_info_id = customer_infos.id LEFT JOIN invoices on invoices.customer_id = customer_infos.user_id left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor where invoices.id IS NOT NULL'; IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND invoices.invoice_date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `regionWiseProductSaleCategory`(IN `org_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = ' SELECT item_major_categories.id,item_major_categories.name from invoice_details LEFT JOIN items on items.id = invoice_details.item_id LEFT JOIN item_major_categories on item_major_categories.id = items.item_major_category_id'; IF org_id > 0 THEN SET otherreplace = ' WHERE item_major_categories.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' GROUP BY item_major_categories.id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `regionWiseProductSaleDetails`(IN `invoice_id` LONGTEXT CHARSET utf8, IN `org_id` INT(11) UNSIGNED, IN `cat_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = ' SELECT SUM(invoice_details.item_grand_total) as prod_sales, item_major_categories.name,invoice_details.item_id,items.item_major_category_id from invoice_details LEFT JOIN items on items.id = invoice_details.item_id LEFT JOIN item_major_categories on item_major_categories.id = items.item_major_category_id '; IF org_id > 0 THEN SET otherreplace = ' WHERE items.organisation_id = orgidss AND item_major_categories.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF invoice_id != '' THEN SET otherreplace = ' AND invoice_details.invoice_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,invoice_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cat_id > 0 THEN SET otherreplace = ' AND items.item_major_category_id = catidss '; SET otherreplace = REPLACE (otherreplace,'catidss',cat_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `regionWiseProductSaleInvoice`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'SELECT invoices.id from invoices left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor'; IF cust_id != '' THEN SET otherreplace = ' WHERE invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND invoices.invoice_date between "sdatess" and "edatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `regionWiseProductSaleInvoiceCombine`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `sup_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8, IN `cust_id` LONGTEXT CHARSET utf8, IN `cat_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'SELECT SUM(invoice_details.item_grand_total) as prod_sales from invoices LEFT JOIN invoice_details on invoice_details.invoice_id = invoices.id LEFT JOIN items on items.id = invoice_details.item_id LEFT JOIN item_major_categories on item_major_categories.id = items.item_major_category_id left join salesman_infos on salesman_infos.user_id = invoices.salesman_id left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor'; IF cust_id != '' THEN SET otherreplace = ' WHERE invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND invoices.invoice_date between "sdatess" and "edatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id != '' THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sup_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cat_id > 0 THEN SET otherreplace = ' AND items.item_major_category_id = catidss '; SET otherreplace = REPLACE (otherreplace,'catidss',cat_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' GROUP BY items.item_major_category_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `resalableCreditNote`(IN `invoice_id` INT(11)) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText ='select credit_notes.credit_note_number,SUM(credit_note_details.item_qty) as Total_return_qty from credit_notes left join credit_note_details on credit_note_details.credit_note_id = credit_notes.id WHERE credit_notes.invoice_id = invoice_idss'; IF invoice_id > 0 THEN SET SQLText = REPLACE (SQLText,'invoice_idss',invoice_id); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `rootMonitoringList`(IN `org_id` INT(11) UNSIGNED, IN `role_id` INT(11) UNSIGNED, IN `user_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = ' select organisation_roles.parent_id, organisation_roles.name as user_role from organisation_role_user_attacheds LEFT JOIN organisation_roles on organisation_roles.id = organisation_role_user_attacheds.role_user_id '; IF org_id > 0 THEN SET otherreplace = ' WHERE organisation_role_user_attacheds.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF role_id > 0 THEN SET otherreplace = ' AND organisation_role_user_attacheds.role_parent_user_id = rolde_idss '; SET otherreplace = REPLACE (otherreplace,'rolde_idss',role_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF user_id > 0 THEN SET otherreplace = ' AND organisation_role_user_attacheds.role_user_id = useridss '; SET otherreplace = REPLACE (otherreplace,'useridss',user_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `rootMonitoringListSalesman`(IN `org_id` INT(11) UNSIGNED, IN `role_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select concat(users.firstname," ",users.lastname) as user_name,salesman_infos.salesman_code from users LEFT JOIN salesman_infos on users.id = salesman_infos.user_id '; IF org_id > 0 THEN SET otherreplace = ' WHERE users.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF role_id > 0 THEN SET otherreplace = ' AND users.role_id = roleidss '; SET otherreplace = REPLACE (otherreplace,'roleidss',role_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `routeComplianceSP`(IN `sdate` DATE, IN `edate` DATE, IN `all_ids` VARCHAR(500) CHARSET utf8) NO SQL BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'SELECT salesman_infos.salesman_supervisor,salesman_infos.salesman_code,customer.firstname as cust_firstname,customer.lastname as cust_lastname,salesman.firstname as sales_firstname,salesman.lastname as sales_lastname, salesman.email as sales_email, customer_visits.id, customer_visits.customer_id,customer_visits.journey_plan_id, customer_visits.salesman_id, customer_visits.latitude, customer_visits.longitude, customer_visits.start_time, customer_visits.end_time, customer_visits.is_sequnece, customer_visits.date, customer_visits.created_at,SUM(CASE WHEN customer_visits.journey_plan_id > 0 THEN 1 ELSE 0 END) as total_journey,SUM(CASE WHEN is_sequnece = "1" THEN 1 ELSE 0 END) as planed_journey,SUM(CASE WHEN is_sequnece = "0" THEN 1 ELSE 0 END) as un_planed_journey from customer_visits LEFT JOIN users as customer on customer.id = customer_visits.customer_id LEFT JOIN users as salesman on salesman.id = customer_visits.salesman_id LEFT JOIN salesman_infos on salesman_infos.user_id = salesman.id'; IF (sdate = edate) THEN SET otherreplace = ' WHERE customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' WHERE customer_visits.date between "sdatess" and ''edatess'' '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' WHERE customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(customer_visits.salesman_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' GROUP BY customer_visits.salesman_id, customer_visits.date, customer_visits.customer_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `salesmanDailySalesReport`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `all_ids` VARCHAR(500) CHARSET utf8, IN `div_this` VARCHAR(500) CHARSET utf8, IN `region_this` VARCHAR(500) CHARSET utf8, IN `route_this` VARCHAR(500) CHARSET utf8, IN `supervisor_this` VARCHAR(500) CHARSET utf8, IN `salesman_this` VARCHAR(500) CHARSET utf8, IN `start_limit` INT(11) UNSIGNED, IN `end_limit` INT(11) UNSIGNED) BEGIN DECLARE otherreplace varchar(500); DECLARE SQLText varchar(8000); SET SQLText = 'select salesman_infos.user_id,salesman_infos.id, salesman_infos.created_at, salesman_infos.user_id, salesman_infos.salesman_code,concat(users.firstname," ",users.lastname) as salesman_name FROM salesman_infos left join salesman_lobs on salesman_lobs.salesman_info_id = salesman_infos.id LEFT JOIN users on users.id = salesman_infos.user_id where salesman_infos.deleted_at is null '; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = orgidss'; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF all_ids != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "otherid")'; SET otherreplace = REPLACE (otherreplace,'otherid',all_ids); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF div_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_lobs.lob_id, "divthis_ids")'; SET otherreplace = REPLACE (otherreplace,'divthis_ids',div_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF region_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.region_id, "regionthis_ids")'; SET otherreplace = REPLACE (otherreplace,'regionthis_ids',region_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF route_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.route_id, "routethis_ids")'; SET otherreplace = REPLACE (otherreplace,'routethis_ids',route_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF supervisor_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.salesman_supervisor, "supervisorthis_ids")'; SET otherreplace = REPLACE (otherreplace,'supervisorthis_ids',supervisor_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF salesman_this != '' THEN SET otherreplace = ' AND FIND_IN_SET(salesman_infos.user_id, "salesmanthis_ids")'; SET otherreplace = REPLACE (otherreplace,'salesmanthis_ids',salesman_this); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' GROUP BY salesman_infos.user_id ORDER BY salesman_infos.created_at'; SET SQLText = CONCAT(SQLText,otherreplace); IF start_limit > 0 AND end_limit <= 0 THEN SET otherreplace = ' LIMIT end_limits , start_limits'; SET otherreplace = REPLACE (otherreplace,'start_limits',start_limit); SET otherreplace = REPLACE (otherreplace,'end_limits',end_limit); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `salesmanDailyWiseInvoiceDetails`(IN `sdate` DATE, IN `user_id` INT(11) UNSIGNED, IN `org_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select invoices.invoice_number,invoices.grand_total, customer_infos.customer_code, concat(users.firstname," ",users.lastname) as customer_name from invoices LEFT JOIN customer_infos on customer_infos.user_id = invoices.customer_id LEFT JOIN users on users.id = customer_infos.user_id WHERE invoices.deleted_at is null '; IF sdate IS NOT NULL THEN SET otherreplace = ' AND invoices.invoice_date = "sdatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF user_id > 0 THEN SET otherreplace = ' AND invoices.salesman_id = useridss '; SET otherreplace = REPLACE (otherreplace,'useridss',user_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' GROUP BY invoices.id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `salesmanDailyWiseSalesReport`(IN `sdate` DATE, IN `user_id` INT(11) UNSIGNED, IN `org_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select SUM(invoices.grand_total) as total_sales from invoices WHERE invoices.deleted_at is null'; IF sdate IS NOT NULL THEN SET otherreplace = ' AND invoices.invoice_date = "sdatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF user_id > 0 THEN SET otherreplace = ' AND invoices.salesman_id = useridss '; SET otherreplace = REPLACE (otherreplace,'useridss',user_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND invoices.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `salesmanDailyWiseSalesReturnReport`(IN `sdate` DATE, IN `user_id` INT(11) UNSIGNED, IN `org_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select SUM(credit_notes.grand_total) as total_return from credit_notes WHERE credit_notes.deleted_at is null'; IF sdate IS NOT NULL THEN SET otherreplace = ' AND credit_notes.credit_note_date = "sdatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF user_id > 0 THEN SET otherreplace = ' AND credit_notes.salesman_id = useridss '; SET otherreplace = REPLACE (otherreplace,'useridss',user_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF org_id > 0 THEN SET otherreplace = ' AND credit_notes.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `salesmanIdGet`(IN `org_id` INT(11) UNSIGNED) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select user_id from salesman_infos'; IF org_id > 0 THEN SET otherreplace = ' WHERE organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_add_routegroup`(IN `routeid` INT(11), IN `groupid` INT(11)) BEGIN DECLARE cnt INT; SELECT COUNT(*) INTO @cnt FROM route_group WHERE routeid =routeid AND groupid =groupid ; IF @cnt = 0 THEN INSERT INTO route_group(routeid,groupid) VALUES(routeid,groupid); END IF; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_get_achivement`( IN userid INT, IN monthid INT) BEGIN DECLARE ince INT; DECLARE baseamount INT; DECLARE base2amount INT; DECLARE base3amount INT; DECLARE targetval INT; DECLARE gtotal INT; DECLARE ctotal INT; DECLARE routeid INT; select `route_id` into routeid from `salesman_infos` where `user_id` = userid; Select incentive INTO ince from `salesman_infos` where id = 1; SELECT Targetvalue into targetval FROM route_targets WHERE route_id = routeid AND monthid <= MONTH(StartDate) AND monthid >= MONTH(Enddate) AND Applyon = 1; SELECT SUM(grand_total) into gtotal FROM invoices WHERE route_id = routeid AND monthid = MONTH(invoice_date); SELECT SUM(grand_total) into ctotal FROM credit_notes WHERE route_id = routeid AND monthid = MONTH(credit_note_date); SELECT (ince *60 /100) as incentive1 ,(ince *30 /100) AS incentive2,(ince *10 /100) AS incentive3, ince as inc,targetval as salestarge,gtotal as achivetarget,ctotal as totalreturn,(gtotal - targetval) as surplus,((gtotal/targetval) *100) AS per, IF(((gtotal/targetval) *100) >= 90.0000,((ince *60 /100) *75 /100),0 ) as incentive ; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_get_incentive`( IN userid INT, IN monthid INT) BEGIN DECLARE ince INT; DECLARE baseamount INT; DECLARE base2amount INT; DECLARE base3amount INT; DECLARE targetval INT; DECLARE gtotal INT; DECLARE routeid INT; select `route_id` into routeid from `salesman_infos` where `user_id` = userid; Select incentive INTO ince from `salesman_infos` where id = 1; SELECT Targetvalue into targetval FROM route_targets WHERE route_id = routeid AND monthid <= MONTH(StartDate) AND monthid >= MONTH(Enddate) AND Applyon = 1; SELECT SUM(grand_total) into gtotal FROM invoices WHERE route_id = routeid AND monthid = MONTH(invoice_date); SELECT (ince *60 /100) as incentive1 ,(ince *30 /100) AS incentive2,(ince *10 /100) AS incentive3, ince as inc,targetval as salestarge,gtotal as achivetarget,(gtotal - targetval) as surplus,((gtotal/targetval) *100) AS per, IF(((gtotal/targetval) *100) >= 90.0000,((ince *60 /100) *75 /100),0 ) as incentive ; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_get_incentive2`(IN userid INT,IN monthid INT) BEGIN DECLARE ince INT ; DECLARE baseamount INT ; DECLARE base2amount INT ; DECLARE categcnt INT ; DECLARE targetval INT ; DECLARE targetqty INT ; DECLARE gtotal INT ; DECLARE achiv INT ; DECLARE routeid INT; select `route_id` into routeid from `salesman_infos` where `user_id` = userid; SELECT incentive INTO ince FROM `salesman_infos` WHERE id = 1 ; SELECT count(id) INTO categcnt FROM `route_target_details` WHERE routes_target_id IN (SELECT id FROM route_targets WHERE route_id = routeid AND monthid <= MONTH(StartDate) AND monthid >= MONTH(Enddate) AND Applyon = 2 ); SELECT fixed_value INTO targetqty FROM `route_target_details` WHERE routes_target_id IN (SELECT id FROM route_targets WHERE route_id = routeid AND monthid <= MONTH(StartDate) AND monthid >= MONTH(Enddate) AND Applyon = 2 ) limit 1; SELECT SUM(item_qty) INTO achiv FROM `invoice_details` WHERE `invoice_id` IN (SELECT id FROM invoices WHERE route_id = 1 AND monthid = MONTH(invoice_date)) AND `item_id` IN (SELECT id FROM items WHERE item_major_category_id IN (SELECT category_id FROM `route_target_details` WHERE routes_target_id IN (SELECT id FROM route_targets WHERE route_id = routeid))) ; SELECT item_name,targetqty,achiv,if(targetqty <= achiv,'Yes','No') as sts, (targetqty - achiv) as surpls,IF(targetqty <= achiv,((ince *30 /100)/categcnt) ,'0') AS sts FROM items WHERE item_major_category_id IN (SELECT category_id FROM `route_target_details` WHERE routes_target_id IN (SELECT id FROM route_targets WHERE route_id = routeid)) ; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_get_incentive_categ`(IN userid INT,IN monthid INT,IN route_details_id INT) BEGIN DECLARE ince INT ; DECLARE baseamount INT ; DECLARE base2amount INT ; DECLARE categcnt INT ; DECLARE targetval INT ; DECLARE targetqty INT ; DECLARE gtotal INT ; DECLARE achiv INT ; DECLARE categ INT ; DECLARE rtid INT ; DECLARE cname varchar(255) ; DECLARE routeid INT; #GEt Route id SELECT `route_id` INTO routeid FROM `salesman_infos` WHERE `user_id` = userid; #Get Route targe id SELECT id INTO rtid FROM route_targets WHERE route_id = routeid AND monthid <= MONTH(StartDate) AND monthid >= MONTH(Enddate) and id in (route_details_id) ; # Get Incentive SELECT incentive INTO ince FROM `salesman_infos` WHERE `user_id` = userid; #Get Category id SELECT `category_id` INTO categ FROM `route_target_details` WHERE id in (route_details_id); #Get Total Category SELECT COUNT(id) INTO categcnt FROM `route_target_details` WHERE routes_target_id IN (rtid); #Get Target Value SELECT fixed_value INTO targetqty FROM `route_target_details` WHERE id in (route_details_id); #Get Acthive Qty SELECT SUM(item_qty) INTO achiv FROM `invoice_details` WHERE `invoice_id` IN (SELECT id FROM invoices WHERE route_id = routeid AND monthid = MONTH(invoice_date)) AND `item_id` IN (SELECT id FROM items WHERE item_major_category_id IN (categ)) ; #Get final out put SELECT `item_major_categories`.`name` as cname, categ as category,targetqty,achiv,IF(targetqty <= achiv,'Yes','No') AS sts, (targetqty - achiv) AS surpls,IF(targetqty <= achiv,((ince *30 /100)/categcnt) ,'0') AS sts FROM route_target_details JOIN `item_major_categories` ON `item_major_categories`.`id` = categ and route_target_details.id in (route_details_id); END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_get_pending_invoice`(IN `routeid` VARCHAR(255), IN `org_id` INT) BEGIN SELECT 'Credit' itype,a.id AS invid,a.credit_note_number AS invoicenumber,a.pdc_amount AS pdcAmount,b.customer_code,CONCAT(u.firstname,u.lastname) AS customername,a.customer_id,a.credit_note_date AS invdate,a.credit_note_date AS invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `credit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE b.organisation_id =org_id and b.customer_group_id IN (SELECT DISTINCT groupid FROM route_group WHERE routeid IN (routeid)) OR b.id IN (SELECT DISTINCT customer_id FROM customer_routes WHERE route_id IN (routeid)) UNION SELECT 'Invoice' itype,a.id AS invid,a.invoice_number AS invoicenumber,a.pdc_amount AS pdcAmount,b.customer_code,CONCAT(u.firstname,u.lastname) AS customername,a.customer_id,a.invoice_date AS invdate,a.invoice_due_date AS invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `invoices` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE b.organisation_id =org_id and b.customer_group_id IN (SELECT DISTINCT groupid FROM route_group WHERE routeid IN (routeid) ) OR b.id IN (SELECT DISTINCT customer_id FROM customer_routes WHERE route_id IN (routeid)) UNION SELECT 'Debit' itype,a.id AS invid,a.`debit_note_number` AS invoicenumber,a.pdc_amount AS pdcAmount,b.customer_code,CONCAT(u.firstname,u.lastname) AS customername,a.customer_id,a.debit_note_date AS invdate,a.debit_note_date AS invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,a.`debit_note_type` AS debittype FROM `debit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE b.organisation_id =org_id and b.customer_group_id IN (SELECT DISTINCT groupid FROM route_group WHERE routeid IN (routeid) ) OR b.id IN (SELECT DISTINCT customer_id FROM customer_routes WHERE route_id IN (routeid)); END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_get_pending_invoice_customer`(IN `customerid` INT, IN `lobid` INT, IN `sdate` DATE, IN `edate` DATE) BEGIN SELECT 'Credit' itype,a.id as invid,a.credit_note_number AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.credit_note_date as invdate,a.credit_note_date as invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `credit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid AND a.lob_id = lobid AND a.created_at BETWEEN sdate AND edate UNION SELECT 'Invoice' itype,a.id as invid,a.invoice_number AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.invoice_date AS invdate,a.invoice_due_date AS invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `invoices` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid AND a.lob_id = lobid AND a.created_at BETWEEN sdate AND edate UNION SELECT 'Debit' itype,a.id as invid,a.`debit_note_number` AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.debit_note_date as invdate,a.debit_note_date as invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,a.`debit_note_type` AS debittype FROM `debit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid AND a.lob_id = lobid AND a.created_at BETWEEN sdate AND edate ; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_get_pending_invoice_customer_id`(IN `customerid` INT(11) UNSIGNED) BEGIN SELECT 'Credit' itype,a.id as invid,a.credit_note_number AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.credit_note_date as invdate,a.credit_note_date as invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount, a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `credit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid UNION SELECT 'Invoice' itype,a.id as invid,a.invoice_number AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.invoice_date AS invdate,a.invoice_due_date AS invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount, a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `invoices` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid UNION SELECT 'Debit' itype,a.id as invid,a.`debit_note_number` AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.debit_note_date as invdate,a.debit_note_date as invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount, a.`total_net` AS netamount,a.`total_vat` AS vatamount,a.`debit_note_type` AS debittype FROM `debit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid ; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_get_pending_invoice_customer_lobid`(IN `customerid` INT, IN `lobid` INT) BEGIN SELECT 'Credit' itype,a.id as invid,a.credit_note_number AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.credit_note_date as invdate,a.credit_note_date as invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `credit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid AND a.lob_id = lobid UNION SELECT 'Invoice' itype,a.id as invid,a.invoice_number AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.invoice_date AS invdate,a.invoice_due_date AS invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `invoices` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid AND a.lob_id = lobid UNION SELECT 'Debit' itype,a.id as invid,a.`debit_note_number` AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.debit_note_date as invdate,a.debit_note_date as invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,a.`debit_note_type` AS debittype FROM `debit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid AND a.lob_id = lobid; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_get_pending_invoice_customer_one_id`(IN `customerid` INT(11) UNSIGNED) NO SQL BEGIN SELECT 'Credit' itype,a.id as invid,a.credit_note_number AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.credit_note_date as invdate,a.credit_note_date as invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount, a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `credit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid UNION SELECT 'Invoice' itype,a.id as invid,a.invoice_number AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.invoice_date AS invdate,a.invoice_due_date AS invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount, a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `invoices` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid UNION SELECT 'Debit' itype,a.id as invid,a.`debit_note_number` AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.debit_note_date as invdate,a.debit_note_date as invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount, a.`total_net` AS netamount,a.`total_vat` AS vatamount,a.`debit_note_type` AS debittype FROM `debit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE a.customer_id = customerid ; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_get_pending_invoice_group`(IN `groupid` INT, IN `org_id` INT) BEGIN SELECT 'Credit' itype,a.id AS invid,a.credit_note_number AS invoicenumber,b.customer_code,CONCAT(u.firstname,u.lastname) AS customername,a.customer_id,a.credit_note_date AS invdate,a.credit_note_date AS invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `credit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE b.organisation_id =org_id AND b.customer_group_id IN (groupid) UNION SELECT 'Invoice' itype,a.id AS invid,a.invoice_number AS invoicenumber,b.customer_code,CONCAT(u.firstname,u.lastname) AS customername,a.customer_id,a.invoice_date AS invdate,a.invoice_due_date AS invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `invoices` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE b.organisation_id =org_id AND b.customer_group_id IN (groupid) UNION SELECT 'Debit' itype,a.id AS invid,a.`debit_note_number` AS invoicenumber,b.customer_code,CONCAT(u.firstname,u.lastname) AS customername,a.customer_id,a.debit_note_date AS invdate,a.debit_note_date AS invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,a.`debit_note_type` AS debittype FROM `debit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE b.organisation_id =org_id AND b.customer_group_id IN (groupid); END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `sp_get_pending_invoice_group_date`(IN `groupid` INT, IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT) BEGIN SELECT 'Credit' itype,a.id as invid,a.credit_note_number AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.credit_note_date as invdate,a.credit_note_date as invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `credit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE b.organisation_id =org_id AND b.customer_group_id in (groupid) and a.created_at BETWEEN sdate AND edate UNION SELECT 'Invoice' itype,a.id as invid,a.invoice_number AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.invoice_date AS invdate,a.invoice_due_date AS invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,'' AS debittype FROM `invoices` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE b.organisation_id =org_id AND b.customer_group_id in (groupid) and a.created_at BETWEEN sdate AND edate UNION SELECT 'Debit' itype,a.id as invid,a.`debit_note_number` AS invoicenumber,b.customer_code,Concat(u.firstname,u.lastname) as customername,a.customer_id,a.debit_note_date as invdate,a.debit_note_date as invduedate,a.grand_total,a.lob_id,b.customer_group_id,a.`pending_credit` AS pending_amount,a.`total_net` AS netamount,a.`total_vat` AS vatamount,a.`debit_note_type` AS debittype FROM `debit_notes` a INNER JOIN customer_infos b ON a.customer_id = b.user_id AND a.`pending_credit`>0 INNER JOIN users u ON b.user_id =u.id WHERE b.organisation_id =org_id AND b.customer_group_id in (groupid) and a.created_at BETWEEN sdate AND edate; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `supervisorTotalLoadItems`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sup_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select count(DISTINCT load_request_details.item_id) as total_items_load from salesman_infos left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor LEFT JOIN load_requests on load_requests.salesman_id = salesman_infos.user_id LEFT JOIN load_request_details on load_request_details.load_request_id = load_requests.id'; IF org_id > 0 THEN SET otherreplace = ' WHERE salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sdate IS NOT NULL THEN SET otherreplace = ' AND load_requests.load_date = "sdatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id > 0 THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor = supidss '; SET otherreplace = REPLACE (otherreplace,'supidss',sup_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `supervisorTotalOrders`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sup_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select SUM(orders.grand_total) as total_orders from salesman_infos left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor LEFT JOIN orders on orders.salesman_id = salesman_infos.user_id'; IF org_id > 0 THEN SET otherreplace = ' WHERE salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sdate IS NOT NULL THEN SET otherreplace = ' AND orders.order_date = "sdatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id > 0 THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor = supidss '; SET otherreplace = REPLACE (otherreplace,'supidss',sup_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND orders.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `supervisorTotalReturn`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sup_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select SUM(credit_notes.grand_total) as total_return from salesman_infos left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor LEFT JOIN credit_notes on credit_notes.salesman_id = salesman_infos.user_id'; IF org_id > 0 THEN SET otherreplace = ' WHERE salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sdate IS NOT NULL THEN SET otherreplace = ' AND credit_notes.credit_note_date = "sdatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id > 0 THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor = supidss '; SET otherreplace = REPLACE (otherreplace,'supidss',sup_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND credit_notes.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `supervisorTotalSales`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sup_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select SUM(invoices.grand_total) as total_sales from salesman_infos left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor LEFT JOIN invoices on invoices.salesman_id = salesman_infos.user_id'; IF org_id > 0 THEN SET otherreplace = ' WHERE salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sdate IS NOT NULL THEN SET otherreplace = ' AND invoices.invoice_date = "sdatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id > 0 THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor = supidss '; SET otherreplace = REPLACE (otherreplace,'supidss',sup_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND invoices.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `supervisorTotalVisit`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `sup_id` INT(11) UNSIGNED, IN `sales_id` LONGTEXT CHARSET utf8, IN `nsm_id` LONGTEXT CHARSET utf8, IN `channel_ids` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select COUNT(customer_visits.id) as visitshop from salesman_infos left join organisation_role_user_attacheds as attached on attached.role_user_id = salesman_infos.salesman_supervisor LEFT JOIN customer_visits on customer_visits.salesman_id = salesman_infos.user_id'; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sdate IS NOT NULL THEN SET otherreplace = ' AND customer_visits.added_on = "sdatess"'; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sup_id > 0 THEN SET otherreplace = ' AND salesman_infos.salesman_supervisor = supidss '; SET otherreplace = REPLACE (otherreplace,'supidss',sup_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF sales_id != '' THEN SET otherreplace = ' AND salesman_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,sales_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF nsm_id != '' THEN SET otherreplace = ' AND attached.role_parent_user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,nsm_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF channel_ids != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,channel_ids); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `visitFrequency_nsm_trends`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select customer_visits.added_on as date, ROUND(COUNT(DISTINCT customer_visits.id)) as value from salesman_infos inner join customer_routes on customer_routes.route_id = salesman_infos.route_id inner join customer_infos on customer_infos.id = customer_routes.id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id where customer_visits.shop_status = "open" and customer_visits.reason is null '; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL AND edate != '0000-00-00' AND sdate != '0000-00-00') THEN SET otherreplace = ' AND customer_visits.date between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.date = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.date order by customer_visits.added_on asc'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `visitPerDay_channel_listing`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesman_infos.salesman_supervisor AS supervisor,channels.name AS channel,regions.region_name AS region,customer_visits.is_sequnece AS sequnece,SUM(CASE WHEN customer_visits.is_sequnece > 0 THEN 1 ELSE 0 END) as unplanned,SUM(CASE WHEN customer_visits.is_sequnece > 1 THEN 1 ELSE 0 END) as visit,customer_visits.latitude AS latitude,customer_visits.longitude AS longitude from channels inner join customer_infos on customer_infos.channel_id = channels.id inner join customer_routes on customer_routes.customer_id = customer_infos.id inner join salesman_infos on salesman_infos.route_id = customer_routes.route_id inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_categories on customer_categories.id = customer_infos.customer_category_id inner join regions on regions.id = customer_infos.region_id where customer_visits.shop_status = "open" and customer_visits.reason is null '; IF org_id > 0 THEN SET otherreplace = ' AND channels.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.added_on between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.id'; SET SQLText = CONCAT(SQLText,otherreplace); SELECT SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `visitPerDay_customerDetails_elsepart`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8, IN `sql_query` LONGTEXT CHARSET utf8, IN `group_by` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = sql_query; IF org_id > 0 THEN SET otherreplace = ' AND customer_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.added_on between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = group_by; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `visitPerDay_listing_else_part`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesmanSupervisor.firstname AS supervisor,channels.name AS channel,regions.region_name AS region,SUM(CASE WHEN customer_visits.is_sequnece > 0 THEN 1 ELSE 0 END) as unplanned,SUM(CASE WHEN customer_visits.is_sequnece > 1 THEN 1 ELSE 0 END) as visit,customer_visits.latitude AS latitude,customer_visits.longitude AS longitude from salesman_infos inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_routes on customer_routes.route_id = salesman_infos.route_id inner join customer_infos on customer_infos.id = customer_routes.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join users as salesmanSupervisor on salesmanSupervisor.id = salesman_infos.salesman_supervisor inner join customer_visits on customer_visits.customer_id = customer_infos.user_id inner join customer_lobs on customer_lobs.customer_info_id = customer_infos.id inner join customer_categories on customer_categories.id = customer_lobs.customer_category_id inner join channels on channels.id = customer_lobs.channel_id inner join regions on regions.id = customer_lobs.region_id where customer_visits.shop_status = "open" and customer_visits.reason is null'; IF org_id > 0 THEN SET otherreplace = ' AND customer_visits.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.added_on between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.added_on, customer_visits.salesman_id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `visitPerDay_regions_listing`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select DISTINCT DATE(customer_visits.added_on) as date,customerInfo.firstname AS customer,customer_infos.customer_code AS customerCode,customer_categories.customer_category_name AS category,salesman.firstname AS merchandiser,salesman_infos.salesman_supervisor AS supervisor,channels.name AS channel,regions.region_name AS region,SUM(CASE WHEN customer_visits.is_sequnece > 0 THEN 1 ELSE 0 END) as unplanned,SUM(CASE WHEN customer_visits.is_sequnece > 1 THEN 1 ELSE 0 END) as visit,customer_visits.is_sequnece AS sequnece,customer_visits.latitude AS latitude,customer_visits.longitude AS longitude from regions inner join customer_infos on customer_infos.region_id = regions.id inner join customer_routes on customer_routes.customer_id = customer_infos.id inner join salesman_infos on salesman_infos.route_id = customer_routes.route_id inner join users as salesman on salesman.id = salesman_infos.user_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id inner join users as customerInfo on customerInfo.id = customer_infos.user_id inner join customer_categories on customer_categories.id = customer_infos.customer_category_id inner join channels on channels.id = customer_infos.channel_id where customer_visits.shop_status = "open" and customer_visits.reason is null '; IF org_id > 0 THEN SET otherreplace = ' AND regions.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.added_on between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_infos.user_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.id'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `visitPerDay_trendsdata_elsepart`(IN `sdate` DATE, IN `edate` DATE, IN `org_id` INT(11) UNSIGNED, IN `cust_id` LONGTEXT CHARSET utf8) NO SQL BEGIN DECLARE otherreplace LONGTEXT; DECLARE SQLText LONGTEXT; SET SQLText = 'select customer_visits.added_on as date, ROUND(COUNT(DISTINCT customer_visits.id)) as value from salesman_infos inner join customer_routes on customer_routes.route_id = salesman_infos.route_id inner join customer_infos on customer_infos.id = customer_routes.customer_id inner join customer_visits on customer_infos.user_id = customer_visits.customer_id where customer_visits.shop_status = "open" and customer_visits.reason is null'; IF org_id > 0 THEN SET otherreplace = ' AND salesman_infos.organisation_id = orgidss '; SET otherreplace = REPLACE (otherreplace,'orgidss',org_id); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF (sdate = edate) THEN SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); ELSEIF (sdate IS NOT NULL AND edate IS NOT NULL) THEN SET otherreplace = ' AND customer_visits.added_on between "sdatess" and "edatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET otherreplace = REPLACE (otherreplace,'edatess',edate); SET SQLText = CONCAT(SQLText,otherreplace); ELSE SET otherreplace = ' AND customer_visits.added_on = "sdatess" '; SET otherreplace = REPLACE (otherreplace,'sdatess',sdate); SET SQLText = CONCAT(SQLText,otherreplace); END IF; IF cust_id != '' THEN SET otherreplace = ' AND customer_visits.customer_id IN ('; SET SQLText = CONCAT(SQLText,otherreplace); SET SQLText = CONCAT(SQLText,cust_id); SET otherreplace = ')'; SET SQLText = CONCAT(SQLText,otherreplace); END IF; SET otherreplace = ' group by customer_visits.date order by customer_visits.added_on asc'; SET SQLText = CONCAT(SQLText,otherreplace); SET @SQLText = SQLText; PREPARE stmt FROM @SQLText; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ DELIMITER ;