PROCEDURE

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 36

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `clientsTripsBasedOnLoadDate`(IN


`from_date` DATE, IN `to_date` DATE)
BEGIN
TRUNCATE tempo_client_total_trip_by_loading_dates;
INSERT INTO tempo_client_total_trip_by_loading_dates
(client_code,client,number_of_trips,number_of_trucks,total_loaded_quantity,client_i
d,cargo_group)
(
SELECT
client_code,
client,
COUNT(DISTINCT loading_order_item_id) as number_of_trips,
COUNT(DISTINCT truck_id) as number_of_trucks,
SUM(loading_quantity) as total_loaded_quantity,
client_id,
cargo_group
FROM trip_loading_details
WHERE loading_date >=from_date
AND loading_date <=to_date
GROUP BY client_id, cargo_group
);
END$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `createLoadingOrderTotalExpense`(IN
`v_loading_order_id` INT(20) UNSIGNED)
NO SQL
BEGIN
TRUNCATE tempo_loading_orders_expenses;
INSERT INTO tempo_loading_orders_expenses
( loading_order_id,loading_order_item_id, account_id,currency,amount ) (
SELECT
loading_order_items.loading_order_id,
requisition_expenses.loading_order_item_id,
requisition_expenses.account_id,
'TSH' AS currency,
SUM(IF(requisition_expenses.currency = 'TSH',
requisition_expenses.amount, requisition_expenses.amount * 2300 )) AS amount
FROM
requisition_expenses
JOIN loading_order_items ON loading_order_items.id =
requisition_expenses.loading_order_item_id
WHERE
loading_order_items.loading_order_id = v_loading_order_id
GROUP BY
requisition_expenses.loading_order_item_id,
requisition_expenses.account_id
);
END$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `getLoadingOrderExensesByDates`(IN
`v_start_date` DATE, IN `v_end_date` DATE)
SELECT
route_assignment_expenses.loading_order_item_id,
route_assignment_expenses.account_id,
route_assignment_expenses.total_expense_amount,
IF
(
SUM(
IF
( budgeted_expense_amounts.currency = 'TSH',
budgeted_expense_amounts.budgeted_amount, budgeted_expense_amounts.budgeted_amount
* 2300 )),
SUM(
IF
( budgeted_expense_amounts.currency = 'TSH',
budgeted_expense_amounts.budgeted_amount, budgeted_expense_amounts.budgeted_amount
* 2300 )),
0
) AS total_budget_amount,
(route_assignment_expenses.total_expense_amount -
IF
(
SUM(
IF
( budgeted_expense_amounts.currency = 'TSH',
budgeted_expense_amounts.budgeted_amount, budgeted_expense_amounts.budgeted_amount
* 2300 )),
SUM(
IF
( budgeted_expense_amounts.currency = 'TSH',
budgeted_expense_amounts.budgeted_amount, budgeted_expense_amounts.budgeted_amount
* 2300 )),
0
)) AS total_un_budget_amount
FROM
(
SELECT
requisition_expenses.loading_order_item_id,
requisition_expenses.account_id,
requisition_expenses.route_assignment_id,
SUM(IF( requisition_expenses.currency = 'TSH',
requisition_expenses.amount, requisition_expenses.amount * 2300 )) AS
total_expense_amount
FROM
requisition_expenses
JOIN loading_order_items ON loading_order_items.id =
requisition_expenses.loading_order_item_id
JOIN loading_orders ON loading_orders.id =
loading_order_items.loading_order_id
WHERE loading_orders.date>=v_start_date and
loading_orders.date<=v_end_date
GROUP BY
requisition_expenses.loading_order_item_id,
requisition_expenses.account_id
) AS route_assignment_expenses
LEFT JOIN (
SELECT

route_budgeted_expenses.account_id,

route_budgeted_expenses.amount AS budgeted_amount,

route_budgeted_expenses.currency
FROM
route_budgeted_expenses
WHERE
route_id = ( SELECT route_id
FROM route_assignments WHERE id = requisition_expenses.route_assignment_id )
AND ( cargo_type = ( SELECT
cargo_type FROM view_route_assignments WHERE route_assignment_id =
requisition_expenses.route_assignment_id ORDER BY loading_order_item_id LIMIT 1 )
OR cargo_type = 'ALL' ) UNION
(
SELECT

loading_point_accounts.account_id,

loading_point_accounts.amount AS budgeted_amount,

loading_point_accounts.currency
FROM
loading_point_accounts
WHERE

loading_point_accounts.loading_point_id = ( SELECT loading_point_id FROM


view_route_assignments WHERE route_assignment_id =
requisition_expenses.route_assignment_id ORDER BY loading_order_item_id LIMIT 1 )
)
) AS budgeted_expense_amounts ON
budgeted_expense_amounts.account_id = route_assignment_expenses.account_id
GROUP BY
route_assignment_expenses.loading_order_item_id,
route_assignment_expenses.account_id$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `getLoadingOrderItemExpenses`(IN
`v_loading_order_item_id` INT(11) UNSIGNED, IN `v_loading_point_id` INT(11)
UNSIGNED, IN `v_route_id` INT(11) UNSIGNED, IN `v_cargo_type` VARCHAR(100))
DETERMINISTIC
BEGIN
TRUNCATE tempo_loading_order_expenses;
INSERT INTO tempo_loading_order_expenses (
loading_order_item_id,
account_id,
total_expense_amount,
total_budget_amount,
total_un_budget_amount)
(
SELECT
requisition_expenses.loading_order_item_id,
requisition_expenses.account_id,
SUM(IF(requisition_expenses.currency
='TSH',requisition_expenses.amount, requisition_expenses.amount* 2300 )) AS
total_expense_amount,
IF(SUM(IF(budgeted_expense_amounts.currency
='TSH',budgeted_expense_amounts.budgeted_amount,budgeted_expense_amounts.budgeted_a
mount* 2300 )),SUM(IF(budgeted_expense_amounts.currency
='TSH',budgeted_expense_amounts.budgeted_amount,budgeted_expense_amounts.budgeted_a
mount* 2300 )),0) AS total_budget_amount,
(SUM(IF(requisition_expenses.currency
='TSH',requisition_expenses.amount, requisition_expenses.amount* 2300 )) -
IF(SUM(IF(budgeted_expense_amounts.currency
='TSH',budgeted_expense_amounts.budgeted_amount,budgeted_expense_amounts.budgeted_a
mount* 2300 )),SUM(IF(budgeted_expense_amounts.currency
='TSH',budgeted_expense_amounts.budgeted_amount,budgeted_expense_amounts.budgeted_a
mount* 2300 )),0)) AS total_un_budget_amount
FROM
requisition_expenses
LEFT JOIN(
SELECT
route_budgeted_expenses.account_id,
route_budgeted_expenses.amount AS budgeted_amount,
route_budgeted_expenses.currency
FROM
route_budgeted_expenses
where route_id =v_route_id
AND (cargo_type =v_cargo_type OR cargo_type='ALL')
UNION(
SELECT
loading_point_accounts.account_id,
loading_point_accounts.amount AS budgeted_amount,
loading_point_accounts.currency
FROM
loading_point_accounts
WHERE loading_point_accounts.loading_point_id =
v_loading_point_id
)
) AS budgeted_expense_amounts ON budgeted_expense_amounts.account_id =
requisition_expenses.account_id
WHERE
requisition_expenses.loading_order_item_id = v_loading_order_item_id
GROUP BY requisition_expenses.loading_order_item_id,
requisition_expenses.account_id
);
END$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `getRouteOrderExpenses`(IN
`v_route_assignment_id` INT(11))
BEGIN
TRUNCATE tempo_loading_order_expenses;
INSERT INTO tempo_loading_order_expenses ( loading_order_item_id, account_id,
total_expense_amount, total_budget_amount, total_un_budget_amount ) (
SELECT
route_assignment_expenses.loading_order_item_id,
route_assignment_expenses.account_id,
route_assignment_expenses.total_expense_amount,
IF
(
SUM(
IF
( budgeted_expense_amounts.currency = 'TSH',
budgeted_expense_amounts.budgeted_amount, budgeted_expense_amounts.budgeted_amount
* 2300 )),
SUM(
IF
( budgeted_expense_amounts.currency = 'TSH',
budgeted_expense_amounts.budgeted_amount, budgeted_expense_amounts.budgeted_amount
* 2300 )),
0
) AS total_budget_amount,
(route_assignment_expenses.total_expense_amount -
IF
(
SUM(
IF
( budgeted_expense_amounts.currency = 'TSH',
budgeted_expense_amounts.budgeted_amount, budgeted_expense_amounts.budgeted_amount
* 2300 )),
SUM(
IF
( budgeted_expense_amounts.currency = 'TSH',
budgeted_expense_amounts.budgeted_amount, budgeted_expense_amounts.budgeted_amount
* 2300 )),
0
)) AS total_un_budget_amount
FROM
(
SELECT
requisition_expenses.loading_order_item_id,
requisition_expenses.account_id,
requisition_expenses.route_assignment_id,
SUM(IF( requisition_expenses.currency = 'TSH',
requisition_expenses.amount, requisition_expenses.amount *
requisition_expenses.exchange_rate )) AS total_expense_amount
FROM
requisition_expenses
GROUP BY
requisition_expenses.loading_order_item_id,
requisition_expenses.account_id
) AS route_assignment_expenses
LEFT JOIN (
SELECT
route_budgeted_expenses.account_id,
route_budgeted_expenses.amount AS budgeted_amount,
route_budgeted_expenses.currency
FROM
route_budgeted_expenses
WHERE
route_id = ( SELECT route_id FROM route_assignments WHERE
id = v_route_assignment_id )
AND ( cargo_type = ( SELECT cargo_type FROM
view_route_assignments WHERE route_assignment_id = v_route_assignment_id ORDER BY
loading_order_item_id LIMIT 1 ) OR cargo_type = 'ALL' ) UNION
(
SELECT
loading_point_accounts.account_id,
loading_point_accounts.amount AS budgeted_amount,
loading_point_accounts.currency
FROM
loading_point_accounts
WHERE
loading_point_accounts.loading_point_id = ( SELECT
loading_point_id FROM view_route_assignments WHERE route_assignment_id =
v_route_assignment_id ORDER BY loading_order_item_id LIMIT 1 )
)
) AS budgeted_expense_amounts ON
budgeted_expense_amounts.account_id = route_assignment_expenses.account_id
WHERE
route_assignment_expenses.route_assignment_id =
v_route_assignment_id
GROUP BY
route_assignment_expenses.loading_order_item_id,
route_assignment_expenses.account_id
);
END$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `getVendorBalanceAs`(IN `_date_` DATE)
NO SQL
BEGIN
TRUNCATE tempo_vendor_balances;
INSERT INTO tempo_vendor_balances ( vendor_id,amount ) (

SELECT
view_vendor_transactions.vendor_id,
(SUM(
IF(
view_vendor_transactions.action = 'CR',
view_vendor_transactions.amount,
0
)
) -SUM(
IF(
view_vendor_transactions.action = 'DR',
view_vendor_transactions.amount,
0
)
)) AS amount
FROM
view_vendor_transactions WHERE DATE(view_vendor_transactions.date) <= _date_
GROUP BY
view_vendor_transactions.vendor_id
);
END$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `modified_inventory_item_position`(IN
`warehouse_id` INT, IN `selected_item_id` INT)
NO SQL
BEGIN
SELECT
store_items.rate as stock_price,
store_items.currency_id,
store_items.expiry_date,
store_items.id as store_item_id,
store_items.item_id,
store_items.store_id,
SUM(store_items.quantity) AS opening_balance,
COALESCE(
view_received_item_mini.quantity,
0
) AS received_quantity,
COALESCE(
view_issued_item_mini.quantity,
0
) AS issued_quantity,
COALESCE(
view_returned_item_mini.quantity,
0
) AS returned_quantity,
(
SUM(store_items.quantity) + COALESCE(
view_received_item_mini.quantity,
0
) - COALESCE(
view_returned_item_mini.quantity,
0
) - COALESCE(
view_issued_item_mini.quantity,
0
)
) AS closing_balance
FROM
store_items
LEFT JOIN view_received_item_mini ON view_received_item_mini.item_id =
store_items.item_id
LEFT JOIN view_issued_item_mini ON view_issued_item_mini.item_id =
store_items.item_id
LEFT JOIN view_returned_item_mini ON view_returned_item_mini.item_id =
store_items.item_id
WHERE
store_items.store_id = warehouse_id
AND store_items.created_at >= "2018-01-01"
AND store_items.item_id = selected_item_id
GROUP BY
store_items.item_id;
END$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `modified_inventory_position`(IN
`warehouse_id` INT)
NO SQL
BEGIN
SELECT
store_items.rate as stock_price,
store_items.currency_id,
store_items.expiry_date,
store_items.id as store_item_id,
store_items.item_id,
store_items.store_id,
SUM(store_items.quantity) AS opening_balance,
COALESCE(
view_received_item_mini.quantity,
0
) AS received_quantity,
COALESCE(
view_issued_item_mini.quantity,
0
) AS issued_quantity,
COALESCE(
view_returned_item_mini.quantity,
0
) AS returned_quantity,
(
SUM(store_items.quantity) + COALESCE(
view_received_item_mini.quantity,
0
) - COALESCE(
view_returned_item_mini.quantity,
0
) - COALESCE(
view_issued_item_mini.quantity,
0
)
) AS closing_balance
FROM
store_items
LEFT JOIN view_received_item_mini ON view_received_item_mini.item_id =
store_items.item_id
LEFT JOIN view_issued_item_mini ON view_issued_item_mini.item_id =
store_items.item_id
LEFT JOIN view_returned_item_mini ON view_returned_item_mini.item_id =
store_items.item_id
WHERE
store_items.store_id = warehouse_id
AND store_items.created_at >= "2018-01-01"
GROUP BY
store_items.item_id;
END$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `new_inventory_positions`(IN
`from_date` VARCHAR(255), IN `to_date` VARCHAR(255), IN `warehouse_id` INT, IN
`this_item_id` INT)
NO SQL
BEGIN
IF this_item_id IS NULL THEN
SELECT
items.name,
stores.name as warehouse,
unit_of_measurements.name as unit,
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, SUM(IF(asset_quantity IS NULL, 0,
asset_quantity)),
IF(SUM(IF(asset_issued_quantity IS NULL, 0, asset_issued_quantity)) >
0, SUM(IF(asset_quantity IS NULL, 0, asset_quantity)),
SUM(IF(store_items.quantity IS NULL, 0, store_items.quantity)))) as
registered_quantity,
(
((
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, 0,
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0, 0,
SUM(IF(opening_received_quantity IS NULL, 0,
opening_received_quantity))))
- SUM(IF(opening_returned_quantity IS NULL, 0, opening_returned_quantity)))
+
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, 0,
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0, 0,
SUM(IF(store_items.quantity IS NULL, 0, store_items.quantity))))
+
SUM(IF(opening_asset_quantity IS NULL, 0, opening_asset_quantity))
)
-
(
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, SUM(IF(opening_asset_issued_quantity IS NULL,
0, opening_asset_issued_quantity)),
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0,
SUM(IF(opening_asset_issued_quantity IS NULL, 0, opening_asset_issued_quantity)),
SUM(IF(opening_issued_quantity IS NULL, 0,
opening_issued_quantity))))
+ SUM(IF(opening_transferred_quantity IS NULL, 0,
opening_transferred_quantity)))
) as opening_balance,
IF( store_items.quantity IS NULL, 0, SUM(store_items.quantity)) as
store_item_quantity,
IF(asset_quantity IS NULL, 0, SUM(asset_quantity)) as asset_quantity,
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, SUM(IF(asset_quantity IS NULL, 0,
asset_quantity)),
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0,
SUM(IF(asset_quantity IS NULL, 0, asset_quantity)),
SUM(IF(received_quantity IS NULL, 0, received_quantity)))) as
received_quantity,
SUM(IF(returned_quantity IS NULL, 0, returned_quantity)) as returned_quantity,
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, SUM(IF(asset_issued_quantity IS NULL, 0,
asset_issued_quantity)),
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0,
SUM(IF(asset_issued_quantity IS NULL, 0, asset_issued_quantity)),
SUM(IF(issued_quantity IS NULL, 0, issued_quantity)))) as
issued_quantity,
SUM(IF(transferred_quantity IS NULL, 0, transferred_quantity)) as
transferred_quantity,
(
((
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, 0,
IF(SUM(IF(asset_issued_quantity IS NULL, 0, asset_issued_quantity)) >
0, 0,
SUM(IF(received_quantity IS NULL, 0, received_quantity))))
- SUM(IF(returned_quantity IS NULL, 0, returned_quantity)))
+
SUM(IF(asset_quantity IS NULL, 0, asset_quantity))
)
-
(
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, SUM(IF(asset_issued_quantity IS NULL, 0,
asset_issued_quantity)),
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0,
SUM(IF(asset_issued_quantity IS NULL, 0, asset_issued_quantity)),
SUM(IF(issued_quantity IS NULL, 0, issued_quantity))))
+ SUM(IF(transferred_quantity IS NULL, 0, transferred_quantity)))
) as balance,
(
(((
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, 0,
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0, 0,
(SUM(IF(opening_received_quantity IS NULL, 0,
opening_received_quantity)) + SUM(IF(received_quantity IS NULL, 0,
received_quantity)))))
) - (SUM(IF(opening_returned_quantity IS NULL, 0,
opening_returned_quantity)) + SUM(IF(returned_quantity IS NULL, 0,
returned_quantity))) )
+
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, 0,
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0, 0,
SUM(IF(store_items.quantity IS NULL, 0, store_items.quantity))))
+
(SUM(IF(opening_asset_quantity IS NULL, 0, opening_asset_quantity)) +
SUM(IF(asset_quantity IS NULL, 0, asset_quantity)))
)
-
(
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, (SUM(IF(opening_asset_issued_quantity IS NULL,
0, opening_asset_issued_quantity)) + SUM(IF(asset_issued_quantity IS NULL, 0,
asset_issued_quantity)) ),
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0,
(SUM(IF(opening_asset_issued_quantity IS NULL, 0, opening_asset_issued_quantity)) +
SUM(IF(asset_issued_quantity IS NULL, 0, asset_issued_quantity)) ),
(SUM(IF(opening_issued_quantity IS NULL, 0,
opening_issued_quantity)) + SUM(IF(issued_quantity IS NULL, 0, issued_quantity)))))

+
(SUM(IF(opening_transferred_quantity IS NULL, 0,
opening_transferred_quantity)) + SUM(IF(transferred_quantity IS NULL, 0,
transferred_quantity))))
) as closing_balance,
IF(store_items.rate IS NULL, 0, store_items.rate) as stock_price,
store_items.currency_id,
store_items.id as store_item_id,
store_items.item_id,
unit_of_measurements.id as unit_of_measurements_id,
store_items.store_id
FROM (
SELECT
stock_received_entries.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
stock_received_entries.quantity as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_received_notes.created_at
FROM stock_received_entries
JOIN goods_received_notes on goods_received_notes.id =
stock_received_entries.goods_received_note_id AND goods_received_notes.warehouse_id
= warehouse_id
WHERE goods_received_notes.created_at >=from_date
AND goods_received_notes.created_at <=to_date
UNION ALL
SELECT
stock_received_entries.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
stock_received_entries.quantity as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_received_notes.created_at
FROM stock_received_entries
JOIN goods_received_notes on goods_received_notes.id =
stock_received_entries.goods_received_note_id AND goods_received_notes.warehouse_id
= warehouse_id
WHERE goods_received_notes.created_at < from_date
UNION ALL
SELECT
goods_returned_note_items.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
"0" as received_quantity,
goods_returned_note_items.quantity as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_returned_notes.created_at
FROM goods_returned_note_items
JOIN goods_returned_notes ON goods_returned_notes.id =
goods_returned_note_items.goods_returned_note_id
JOIN goods_received_notes ON goods_received_notes.id =
goods_returned_notes.goods_received_note_id AND goods_received_notes.warehouse_id =
warehouse_id
WHERE goods_returned_notes.created_at >=from_date
AND goods_returned_notes.created_at <=to_date
UNION ALL
SELECT
goods_returned_note_items.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
goods_returned_note_items.quantity as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_returned_notes.created_at
FROM goods_returned_note_items
JOIN goods_returned_notes ON goods_returned_notes.id =
goods_returned_note_items.goods_returned_note_id
JOIN goods_received_notes ON goods_received_notes.id =
goods_returned_notes.goods_received_note_id AND goods_received_notes.warehouse_id =
warehouse_id
WHERE goods_returned_notes.created_at < from_date
UNION ALL
SELECT
goods_issued_note_items.item_id,
goods_issued_notes.store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
goods_issued_note_items.quantity as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_issued_notes.created_at
FROM goods_issued_note_items
JOIN goods_issued_notes ON goods_issued_notes.id =
goods_issued_note_items.goods_issued_note_id AND goods_issued_notes.store_id =
warehouse_id
WHERE goods_issued_notes.created_at >=from_date
AND goods_issued_notes.created_at <=to_date
UNION ALL
SELECT
goods_issued_note_items.item_id,
goods_issued_notes.store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
goods_issued_note_items.quantity as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_issued_notes.created_at
FROM goods_issued_note_items
JOIN goods_issued_notes ON goods_issued_notes.id =
goods_issued_note_items.goods_issued_note_id AND goods_issued_notes.store_id =
warehouse_id
WHERE goods_issued_notes.created_at < from_date
UNION ALL
SELECT
transfer_order_items.item_id,
transfer_orders.source_warehouse_id as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as opening_asset_quantity,
requisition_approval_item_items.quantity as transferred_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
transfer_orders.created_at
FROM transfer_order_items
JOIN transfer_order_item_requisition_items ON
transfer_order_item_requisition_items.transfer_order_item_id =
transfer_order_items.id
JOIN requisition_approval_items ON
requisition_approval_items.requisition_item_id =
transfer_order_item_requisition_items.requisition_item_id
JOIN requisition_approval_item_items ON
requisition_approval_item_items.requisition_approval_item_id =
requisition_approval_items.id
JOIN transfer_orders ON transfer_orders.id =
transfer_order_items.transfer_order_id AND transfer_orders.source_warehouse_id =
warehouse_id
WHERE transfer_orders.created_at >=from_date
AND transfer_orders.created_at <=to_date
UNION ALL
SELECT
transfer_order_items.item_id,
transfer_orders.source_warehouse_id as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
requisition_approval_item_items.quantity as opening_transferred_quantity,
transfer_orders.created_at
FROM transfer_order_items
JOIN transfer_order_item_requisition_items ON
transfer_order_item_requisition_items.transfer_order_item_id =
transfer_order_items.id
JOIN requisition_approval_items ON
requisition_approval_items.requisition_item_id =
transfer_order_item_requisition_items.requisition_item_id
JOIN requisition_approval_item_items ON
requisition_approval_item_items.requisition_approval_item_id =
requisition_approval_items.id
JOIN transfer_orders ON transfer_orders.id =
transfer_order_items.transfer_order_id AND transfer_orders.source_warehouse_id =
warehouse_id
WHERE transfer_orders.created_at < from_date
UNION ALL
SELECT
assets.item_id,
NULL as store_id,
assets.quantity as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
assets.created_at
FROM assets
WHERE assets.created_at >=from_date
AND assets.created_at <=to_date
UNION ALL
SELECT
assets.item_id,
NULL as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
assets.quantity as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
assets.created_at
FROM assets
WHERE assets.created_at < from_date
UNION ALL
SELECT
assets.item_id,
NULL as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
IF(assets.quantity IS NULL, 0, assets.quantity) as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
asset_assignments.created_at
FROM asset_assignments
JOIN assets ON asset_assignments.asset_id = assets.id
WHERE asset_assignments.created_at >=from_date
AND asset_assignments.created_at <=to_date
UNION ALL
SELECT
assets.item_id,
NULL as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
IF(assets.quantity IS NULL, 0, assets.quantity) as
opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
asset_assignments.created_at
FROM asset_assignments
JOIN assets ON asset_assignments.asset_id = assets.id
WHERE asset_assignments.created_at < from_date
) as c
RIGHT JOIN store_items ON store_items.item_id = c.item_id
JOIN items ON items.id = store_items.item_id
JOIN stores ON stores.id = store_items.store_id
JOIN unit_of_measurements ON unit_of_measurements.id =
items.unit_of_measurement_id
WHERE store_items.store_id = warehouse_id
GROUP BY store_items.item_id
ORDER BY c.created_at ASC;
ELSE
SELECT
items.name,
stores.name as warehouse,
unit_of_measurements.name as unit,
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, SUM(IF(asset_quantity IS NULL, 0,
asset_quantity)),
IF(SUM(IF(asset_issued_quantity IS NULL, 0, asset_issued_quantity)) >
0, SUM(IF(asset_quantity IS NULL, 0, asset_quantity)),
SUM(IF(store_items.quantity IS NULL, 0, store_items.quantity)))) as
registered_quantity,
(
((
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, 0,
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0, 0,
SUM(IF(opening_received_quantity IS NULL, 0,
opening_received_quantity))))
- SUM(IF(opening_returned_quantity IS NULL, 0, opening_returned_quantity)))
+
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, 0,
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0, 0,
SUM(IF(store_items.quantity IS NULL, 0, store_items.quantity))))
+
SUM(IF(opening_asset_quantity IS NULL, 0, opening_asset_quantity))
)
-
(
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, SUM(IF(opening_asset_issued_quantity IS NULL,
0, opening_asset_issued_quantity)),
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0,
SUM(IF(opening_asset_issued_quantity IS NULL, 0, opening_asset_issued_quantity)),
SUM(IF(opening_issued_quantity IS NULL, 0,
opening_issued_quantity))))
+ SUM(IF(opening_transferred_quantity IS NULL, 0,
opening_transferred_quantity)))
) as opening_balance,
IF( store_items.quantity IS NULL, 0, SUM(store_items.quantity)) as
store_item_quantity,
IF(asset_quantity IS NULL, 0, SUM(asset_quantity)) as asset_quantity,
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, SUM(IF(asset_quantity IS NULL, 0,
asset_quantity)),
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0,
SUM(IF(asset_quantity IS NULL, 0, asset_quantity)),
SUM(IF(received_quantity IS NULL, 0, received_quantity)))) as
received_quantity,
SUM(IF(returned_quantity IS NULL, 0, returned_quantity)) as returned_quantity,
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, SUM(IF(asset_issued_quantity IS NULL, 0,
asset_issued_quantity)),
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0,
SUM(IF(asset_issued_quantity IS NULL, 0, asset_issued_quantity)),
SUM(IF(issued_quantity IS NULL, 0, issued_quantity)))) as
issued_quantity,
SUM(IF(transferred_quantity IS NULL, 0, transferred_quantity)) as
transferred_quantity,
(
((
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, 0,
IF(SUM(IF(asset_issued_quantity IS NULL, 0, asset_issued_quantity)) >
0, 0,
SUM(IF(received_quantity IS NULL, 0, received_quantity))))
- SUM(IF(returned_quantity IS NULL, 0, returned_quantity)))
+
SUM(IF(asset_quantity IS NULL, 0, asset_quantity))
)
-
(
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, SUM(IF(asset_issued_quantity IS NULL, 0,
asset_issued_quantity)),
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0,
SUM(IF(asset_issued_quantity IS NULL, 0, asset_issued_quantity)),
SUM(IF(issued_quantity IS NULL, 0, issued_quantity))))
+ SUM(IF(transferred_quantity IS NULL, 0, transferred_quantity)))
) as balance,
(
(((
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, 0,
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0, 0,
(SUM(IF(opening_received_quantity IS NULL, 0,
opening_received_quantity)) + SUM(IF(received_quantity IS NULL, 0,
received_quantity)))))
) - (SUM(IF(opening_returned_quantity IS NULL, 0,
opening_returned_quantity)) + SUM(IF(returned_quantity IS NULL, 0,
returned_quantity))) )
+
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, 0,
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0, 0,
SUM(IF(store_items.quantity IS NULL, 0, store_items.quantity))))
+
(SUM(IF(opening_asset_quantity IS NULL, 0, opening_asset_quantity)) +
SUM(IF(asset_quantity IS NULL, 0, asset_quantity)))
)
-
(
IF(SUM(IF(opening_asset_issued_quantity IS NULL, 0,
opening_asset_issued_quantity)) > 0, (SUM(IF(opening_asset_issued_quantity IS NULL,
0, opening_asset_issued_quantity)) + SUM(IF(asset_issued_quantity IS NULL, 0,
asset_issued_quantity)) ),
IF(SUM(IF(asset_quantity IS NULL, 0, asset_quantity)) > 0,
(SUM(IF(opening_asset_issued_quantity IS NULL, 0, opening_asset_issued_quantity)) +
SUM(IF(asset_issued_quantity IS NULL, 0, asset_issued_quantity)) ),
(SUM(IF(opening_issued_quantity IS NULL, 0,
opening_issued_quantity)) + SUM(IF(issued_quantity IS NULL, 0, issued_quantity)))))

+
(SUM(IF(opening_transferred_quantity IS NULL, 0,
opening_transferred_quantity)) + SUM(IF(transferred_quantity IS NULL, 0,
transferred_quantity))))
) as closing_balance,
IF(store_items.rate IS NULL, 0, store_items.rate) as stock_price,
store_items.currency_id,
store_items.id as store_item_id,
store_items.item_id,
unit_of_measurements.id as unit_of_measurements_id,
store_items.store_id
FROM (
SELECT
stock_received_entries.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
stock_received_entries.quantity as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_received_notes.created_at
FROM stock_received_entries
JOIN goods_received_notes on goods_received_notes.id =
stock_received_entries.goods_received_note_id AND goods_received_notes.warehouse_id
= warehouse_id
WHERE goods_received_notes.created_at >=from_date
AND goods_received_notes.created_at <=to_date AND stock_received_entries.item_id =
this_item_id
UNION ALL
SELECT
stock_received_entries.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
stock_received_entries.quantity as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_received_notes.created_at
FROM stock_received_entries
JOIN goods_received_notes on goods_received_notes.id =
stock_received_entries.goods_received_note_id AND goods_received_notes.warehouse_id
= warehouse_id
WHERE goods_received_notes.created_at < from_date AND
stock_received_entries.item_id = this_item_id
UNION ALL
SELECT
goods_returned_note_items.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
"0" as received_quantity,
goods_returned_note_items.quantity as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_returned_notes.created_at
FROM goods_returned_note_items
JOIN goods_returned_notes ON goods_returned_notes.id =
goods_returned_note_items.goods_returned_note_id
JOIN goods_received_notes ON goods_received_notes.id =
goods_returned_notes.goods_received_note_id AND goods_received_notes.warehouse_id =
warehouse_id
WHERE goods_returned_notes.created_at >=from_date
AND goods_returned_notes.created_at <=to_date AND goods_returned_note_items.item_id
= this_item_id
UNION ALL
SELECT
goods_returned_note_items.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
goods_returned_note_items.quantity as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_returned_notes.created_at
FROM goods_returned_note_items
JOIN goods_returned_notes ON goods_returned_notes.id =
goods_returned_note_items.goods_returned_note_id
JOIN goods_received_notes ON goods_received_notes.id =
goods_returned_notes.goods_received_note_id AND goods_received_notes.warehouse_id =
warehouse_id
WHERE goods_returned_notes.created_at < from_date AND
goods_returned_note_items.item_id = this_item_id
UNION ALL
SELECT
goods_issued_note_items.item_id,
goods_issued_notes.store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
goods_issued_note_items.quantity as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_issued_notes.created_at
FROM goods_issued_note_items
JOIN goods_issued_notes ON goods_issued_notes.id =
goods_issued_note_items.goods_issued_note_id AND goods_issued_notes.store_id =
warehouse_id
WHERE goods_issued_notes.created_at >=from_date
AND goods_issued_notes.created_at <=to_date AND
goods_issued_note_items.item_id = this_item_id
UNION ALL
SELECT
goods_issued_note_items.item_id,
goods_issued_notes.store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
goods_issued_note_items.quantity as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_issued_notes.created_at
FROM goods_issued_note_items
JOIN goods_issued_notes ON goods_issued_notes.id =
goods_issued_note_items.goods_issued_note_id AND goods_issued_notes.store_id =
warehouse_id
WHERE goods_issued_notes.created_at < from_date AND
goods_issued_note_items.item_id = this_item_id
UNION ALL
SELECT
transfer_order_items.item_id,
transfer_orders.source_warehouse_id as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
requisition_approval_item_items.quantity as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
transfer_orders.created_at
FROM transfer_order_items
JOIN transfer_order_item_requisition_items ON
transfer_order_item_requisition_items.transfer_order_item_id =
transfer_order_items.id
JOIN requisition_approval_items ON
requisition_approval_items.requisition_item_id =
transfer_order_item_requisition_items.requisition_item_id
JOIN requisition_approval_item_items ON
requisition_approval_item_items.requisition_approval_item_id =
requisition_approval_items.id
JOIN transfer_orders ON transfer_orders.id =
transfer_order_items.transfer_order_id AND transfer_orders.source_warehouse_id =
warehouse_id
WHERE transfer_orders.created_at >=from_date
AND transfer_orders.created_at <=to_date AND transfer_order_items.item_id =
this_item_id
UNION ALL
SELECT
transfer_order_items.item_id,
transfer_orders.source_warehouse_id as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
requisition_approval_item_items.quantity as opening_transferred_quantity,
transfer_orders.created_at
FROM transfer_order_items
JOIN transfer_order_item_requisition_items ON
transfer_order_item_requisition_items.transfer_order_item_id =
transfer_order_items.id
JOIN requisition_approval_items ON
requisition_approval_items.requisition_item_id =
transfer_order_item_requisition_items.requisition_item_id
JOIN requisition_approval_item_items ON
requisition_approval_item_items.requisition_approval_item_id =
requisition_approval_items.id
JOIN transfer_orders ON transfer_orders.id =
transfer_order_items.transfer_order_id AND transfer_orders.source_warehouse_id =
warehouse_id
WHERE transfer_orders.created_at < from_date AND transfer_order_items.item_id
= this_item_id
UNION ALL
SELECT
assets.item_id,
NULL as store_id,
assets.quantity as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
assets.created_at
FROM assets
WHERE assets.created_at >=from_date
AND assets.created_at <=to_date
UNION ALL
SELECT
assets.item_id,
NULL as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
assets.quantity as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
assets.created_at
FROM assets
WHERE assets.created_at < from_date
UNION ALL
SELECT
assets.item_id,
NULL as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
IF(assets.quantity IS NULL, 0, assets.quantity) as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
asset_assignments.created_at
FROM asset_assignments
JOIN assets ON asset_assignments.asset_id = assets.id
WHERE asset_assignments.created_at >=from_date
AND asset_assignments.created_at <=to_date
UNION ALL
SELECT
assets.item_id,
NULL as store_id,
"0" as asset_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
IF(assets.quantity IS NULL, 0, assets.quantity) as
opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
asset_assignments.created_at
FROM asset_assignments
JOIN assets ON asset_assignments.asset_id = assets.id
WHERE asset_assignments.created_at < from_date
) as c
RIGHT JOIN store_items ON store_items.item_id = c.item_id
JOIN items ON items.id = store_items.item_id
JOIN stores ON stores.id = store_items.store_id
JOIN unit_of_measurements ON unit_of_measurements.id =
items.unit_of_measurement_id
WHERE store_items.store_id = warehouse_id AND store_items.item_id =
this_item_id
GROUP BY store_items.item_id
ORDER BY c.created_at ASC;
END IF;
END$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `new_item_movements`(IN `from_date`
VARCHAR(255), IN `to_date` VARCHAR(255), IN `warehouse_id` INT, IN `this_item_id`
INT)
NO SQL
BEGIN
SELECT
*
FROM (
SELECT
purchase_orders.id as trasaction_id,
"LPO" as transaction_type,
purchase_orders.date as transaction_date,
purchase_order_items.item_id,
null as store_id,
"0" as asset_quantity,
purchase_order_items.quantity as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
purchase_orders.created_at
FROM purchase_order_items
JOIN purchase_orders on purchase_orders.id = purchase_order_items.purchase_order_id
WHERE purchase_orders.created_at >=from_date
AND purchase_orders.created_at <=to_date AND purchase_order_items.item_id =
this_item_id
UNION ALL
SELECT
purchase_orders.id as trasaction_id,
"LPO_OPEN" as transaction_type,
purchase_orders.date as transaction_date,
purchase_order_items.item_id,
null as store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
purchase_order_items.quantity as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
purchase_orders.created_at
FROM purchase_order_items
JOIN purchase_orders on purchase_orders.id = purchase_order_items.purchase_order_id
WHERE purchase_orders.created_at < from_date AND purchase_order_items.item_id =
this_item_id
UNION ALL
SELECT
goods_received_notes.id as trasaction_id,
"GRN" as transaction_type,
goods_received_notes.created_at as transaction_date,
stock_received_entries.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
stock_received_entries.quantity as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_received_notes.created_at
FROM stock_received_entries
JOIN goods_received_notes on goods_received_notes.id =
stock_received_entries.goods_received_note_id AND goods_received_notes.warehouse_id
= warehouse_id
WHERE goods_received_notes.created_at >=from_date
AND goods_received_notes.created_at <=to_date AND stock_received_entries.item_id =
this_item_id
UNION ALL
SELECT
goods_received_notes.id as trasaction_id,
"GRN_OPEN" as transaction_type,
goods_received_notes.created_at as transaction_date,
stock_received_entries.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
stock_received_entries.quantity as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_received_notes.created_at
FROM stock_received_entries
JOIN goods_received_notes on goods_received_notes.id =
stock_received_entries.goods_received_note_id AND goods_received_notes.warehouse_id
= warehouse_id
WHERE goods_received_notes.created_at < from_date AND
stock_received_entries.item_id = this_item_id
UNION ALL
SELECT
goods_returned_notes.id as trasaction_id,
"RETURN" as transaction_type,
goods_returned_notes.created_at as transaction_date,
goods_returned_note_items.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
goods_returned_note_items.quantity as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_returned_notes.created_at
FROM goods_returned_note_items
JOIN goods_returned_notes ON goods_returned_notes.id =
goods_returned_note_items.goods_returned_note_id
JOIN goods_received_notes ON goods_received_notes.id =
goods_returned_notes.goods_received_note_id AND goods_received_notes.warehouse_id =
warehouse_id
WHERE goods_returned_notes.created_at >=from_date
AND goods_returned_notes.created_at <=to_date AND goods_returned_note_items.item_id
= this_item_id
UNION ALL
SELECT
goods_returned_notes.id as trasaction_id,
"RETURN_OPEN" as transaction_type,
goods_returned_notes.created_at as transaction_date,
goods_returned_note_items.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
goods_returned_note_items.quantity as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_returned_notes.created_at
FROM goods_returned_note_items
JOIN goods_returned_notes ON goods_returned_notes.id =
goods_returned_note_items.goods_returned_note_id
JOIN goods_received_notes ON goods_received_notes.id =
goods_returned_notes.goods_received_note_id AND goods_received_notes.warehouse_id =
warehouse_id
WHERE goods_returned_notes.created_at < from_date AND
goods_returned_note_items.item_id = this_item_id
UNION ALL
SELECT
goods_issued_notes.id as trasaction_id,
"GIN" as transaction_type,
goods_issued_notes.created_at as transaction_date,
goods_issued_note_items.item_id,
goods_issued_notes.store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
goods_issued_note_items.quantity as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_issued_notes.created_at
FROM goods_issued_note_items
JOIN goods_issued_notes ON goods_issued_notes.id =
goods_issued_note_items.goods_issued_note_id AND goods_issued_notes.store_id =
warehouse_id
WHERE goods_issued_notes.created_at >=from_date
AND goods_issued_notes.created_at <=to_date AND
goods_issued_note_items.item_id = this_item_id
UNION ALL
SELECT
goods_issued_notes.id as trasaction_id,
"GIN_OPEN" as transaction_type,
goods_issued_notes.created_at as transaction_date,
goods_issued_note_items.item_id,
goods_issued_notes.store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
goods_issued_note_items.quantity as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
goods_issued_notes.created_at
FROM goods_issued_note_items
JOIN goods_issued_notes ON goods_issued_notes.id =
goods_issued_note_items.goods_issued_note_id AND goods_issued_notes.store_id =
warehouse_id
WHERE goods_issued_notes.created_at < from_date AND
goods_issued_note_items.item_id = this_item_id
UNION ALL
SELECT
transfer_orders.id as trasaction_id,
"TRANSFER" as transaction_type,
transfer_orders.created_at as transaction_date,
transfer_order_items.item_id,
transfer_orders.source_warehouse_id as store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
requisition_approval_item_items.quantity as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
transfer_orders.created_at
FROM transfer_order_items
JOIN transfer_order_item_requisition_items ON
transfer_order_item_requisition_items.transfer_order_item_id =
transfer_order_items.id
JOIN requisition_approval_items ON
requisition_approval_items.requisition_item_id =
transfer_order_item_requisition_items.requisition_item_id
JOIN requisition_approval_item_items ON
requisition_approval_item_items.requisition_approval_item_id =
requisition_approval_items.id
JOIN transfer_orders ON transfer_orders.id =
transfer_order_items.transfer_order_id AND transfer_orders.source_warehouse_id =
warehouse_id
WHERE transfer_orders.created_at >=from_date
AND transfer_orders.created_at <=to_date AND transfer_order_items.item_id =
this_item_id
UNION ALL
SELECT
transfer_orders.id as trasaction_id,
"TRANSFER_OPEN" as transaction_type,
transfer_orders.created_at as transaction_date,
transfer_order_items.item_id,
transfer_orders.source_warehouse_id as store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
requisition_approval_item_items.quantity as opening_transferred_quantity,
transfer_orders.created_at
FROM transfer_order_items
JOIN transfer_order_item_requisition_items ON
transfer_order_item_requisition_items.transfer_order_item_id =
transfer_order_items.id
JOIN requisition_approval_items ON
requisition_approval_items.requisition_item_id =
transfer_order_item_requisition_items.requisition_item_id
JOIN requisition_approval_item_items ON
requisition_approval_item_items.requisition_approval_item_id =
requisition_approval_items.id
JOIN transfer_orders ON transfer_orders.id =
transfer_order_items.transfer_order_id AND transfer_orders.source_warehouse_id =
warehouse_id
WHERE transfer_orders.created_at < from_date AND transfer_order_items.item_id
= this_item_id
UNION ALL
SELECT
assets.id as trasaction_id,
"ASSET" as transaction_type,
assets.created_at as transaction_date,
assets.item_id,
NULL as store_id,
assets.quantity as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
assets.created_at
FROM assets
WHERE assets.created_at >=from_date
AND assets.created_at <=to_date
UNION ALL
SELECT
assets.id as trasaction_id,
"ASSET_OPEN" as transaction_type,
assets.created_at as transaction_date,
assets.item_id,
NULL as store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
assets.quantity as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
assets.created_at
FROM assets
WHERE assets.created_at < from_date
UNION ALL
SELECT
assets.id as trasaction_id,
"ASSET_ASSIGN" as transaction_type,
asset_assignments.created_at as transaction_date,
assets.item_id,
NULL as store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
IF(assets.quantity IS NULL, 0, assets.quantity) as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
asset_assignments.created_at
FROM asset_assignments
JOIN assets ON asset_assignments.asset_id = assets.id
WHERE asset_assignments.created_at >=from_date
AND asset_assignments.created_at <=to_date
UNION ALL
SELECT
assets.id as trasaction_id,
"ASSET_ASSIGN_OPEN" as transaction_type,
asset_assignments.created_at as transaction_date,
assets.item_id,
NULL as store_id,
"0" as asset_quantity,
"0" as purchased_quantity,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as asset_issued_quantity,
"0" as transferred_quantity,
"0" as opening_asset_quantity,
"0" as opening_purchased_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
IF(assets.quantity IS NULL, 0, assets.quantity) as
opening_asset_issued_quantity,
"0" as opening_transferred_quantity,
asset_assignments.created_at
FROM asset_assignments
JOIN assets ON asset_assignments.asset_id = assets.id
WHERE asset_assignments.created_at < from_date
) as c
RIGHT JOIN store_items ON store_items.item_id = c.item_id
JOIN items ON items.id = store_items.item_id
JOIN stores ON stores.id = store_items.store_id
JOIN unit_of_measurements ON unit_of_measurements.id =
items.unit_of_measurement_id
WHERE store_items.store_id = warehouse_id AND store_items.item_id =
this_item_id
GROUP BY c.created_at
ORDER BY c.created_at ASC;
END$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `new_stock_positions`(IN `from_date`
INT, IN `to_date` INT, IN `warehouse_id` INT)
NO SQL
BEGIN
SELECT
items.name,
stores.name as warehouse,
unit_of_measurements.name as unit,
SUM(COALESCE(store_items.quantity)) as registered_quantity,
(
((SUM(IF(opening_received_quantity IS NULL, 0, opening_received_quantity)) -
SUM(IF(opening_returned_quantity IS NULL, 0, opening_returned_quantity)))
+
SUM(IF(store_items.quantity IS NULL, 0, store_items.quantity)))
-
( SUM(IF(opening_issued_quantity IS NULL, 0, opening_issued_quantity)) +
SUM(IF(opening_transferred_quantity IS NULL, 0, opening_transferred_quantity)))
) as opening_balance,
SUM(IF(received_quantity IS NULL, 0, received_quantity)) as received_quantity,
SUM(IF(returned_quantity IS NULL, 0, returned_quantity)) as returned_quantity,
SUM(IF(issued_quantity IS NULL, 0, issued_quantity)) as issued_quantity,
SUM(IF(transferred_quantity IS NULL, 0, transferred_quantity)) as
transferred_quantity,
(
((SUM(IF(received_quantity IS NULL, 0, received_quantity)) -
SUM(IF(returned_quantity IS NULL, 0, returned_quantity)))
+
SUM(IF(store_items.quantity IS NULL, 0, store_items.quantity)))
-
(SUM(IF(issued_quantity IS NULL, 0, issued_quantity)) +
SUM(IF(transferred_quantity IS NULL, 0, transferred_quantity)))
) as balance,
(
(((SUM(IF(opening_received_quantity IS NULL, 0, opening_received_quantity)) +
SUM(IF(received_quantity IS NULL, 0, received_quantity))) -
(SUM(IF(opening_returned_quantity IS NULL, 0, opening_returned_quantity)) +
SUM(IF(returned_quantity IS NULL, 0, returned_quantity))))
+
SUM(IF(store_items.quantity IS NULL, 0, store_items.quantity)))
-
((SUM(IF(opening_issued_quantity IS NULL, 0, opening_issued_quantity)) +
SUM(IF(issued_quantity IS NULL, 0, issued_quantity)))
+
(SUM(IF(opening_transferred_quantity IS NULL, 0,
opening_transferred_quantity)) + SUM(IF(transferred_quantity IS NULL, 0,
transferred_quantity))))
) as closing_balance,
IF(store_items.rate IS NULL, 0, store_items.rate) as stock_price,
store_items.item_id,
unit_of_measurements.id as unit_of_measurements_id,
store_items.store_id
FROM (
SELECT
stock_received_entries.item_id,
goods_received_notes.warehouse_id as store_id,
stock_received_entries.quantity as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as transferred_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_transferred_quantity,
goods_received_notes.created_at
FROM stock_received_entries
JOIN goods_received_notes on goods_received_notes.id =
stock_received_entries.goods_received_note_id AND goods_received_notes.warehouse_id
= warehouse_id
WHERE goods_received_notes.created_at >=from_date
AND goods_received_notes.created_at <=to_date
UNION ALL
SELECT
stock_received_entries.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as transferred_quantity,
stock_received_entries.quantity as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_transferred_quantity,
goods_received_notes.created_at
FROM stock_received_entries
JOIN goods_received_notes on goods_received_notes.id =
stock_received_entries.goods_received_note_id AND goods_received_notes.warehouse_id
= warehouse_id
WHERE goods_received_notes.created_at < from_date
UNION ALL
SELECT
goods_returned_note_items.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as received_quantity,
goods_returned_note_items.quantity as returned_quantity,
"0" as issued_quantity,
"0" as transferred_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_transferred_quantity,
goods_returned_notes.created_at
FROM goods_returned_note_items
JOIN goods_returned_notes ON goods_returned_notes.id =
goods_returned_note_items.goods_returned_note_id
JOIN goods_received_notes ON goods_received_notes.id =
goods_returned_notes.goods_received_note_id AND goods_received_notes.warehouse_id =
warehouse_id
WHERE goods_returned_notes.created_at >=from_date
AND goods_returned_notes.created_at <=to_date
UNION ALL
SELECT
goods_returned_note_items.item_id,
goods_received_notes.warehouse_id as store_id,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as transferred_quantity,
"0" as opening_received_quantity,
goods_returned_note_items.quantity as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_transferred_quantity,
goods_returned_notes.created_at
FROM goods_returned_note_items
JOIN goods_returned_notes ON goods_returned_notes.id =
goods_returned_note_items.goods_returned_note_id
JOIN goods_received_notes ON goods_received_notes.id =
goods_returned_notes.goods_received_note_id AND goods_received_notes.warehouse_id =
warehouse_id
WHERE goods_returned_notes.created_at < from_date
UNION ALL
SELECT
goods_issued_note_items.item_id,
goods_issued_notes.store_id,
"0" as received_quantity,
"0" as returned_quantity,
goods_issued_note_items.quantity as issued_quantity,
"0" as transferred_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_transferred_quantity,
goods_issued_notes.created_at
FROM goods_issued_note_items
JOIN goods_issued_notes ON goods_issued_notes.id =
goods_issued_note_items.goods_issued_note_id AND goods_issued_notes.store_id =
warehouse_id
WHERE goods_issued_notes.created_at >=from_date
AND goods_issued_notes.created_at <=to_date
UNION ALL
SELECT
goods_issued_note_items.item_id,
goods_issued_notes.store_id,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as transferred_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
goods_issued_note_items.quantity as opening_issued_quantity,
"0" as opening_transferred_quantity,
goods_issued_notes.created_at
FROM goods_issued_note_items
JOIN goods_issued_notes ON goods_issued_notes.id =
goods_issued_note_items.goods_issued_note_id AND goods_issued_notes.store_id =
warehouse_id
WHERE goods_issued_notes.created_at < from_date
UNION ALL
SELECT
transfer_order_items.item_id,
transfer_orders.source_warehouse_id as store_id,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
requisition_approval_item_items.quantity as transferred_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
"0" as opening_transferred_quantity,
transfer_orders.created_at
FROM transfer_order_items
JOIN transfer_order_item_requisition_items ON
transfer_order_item_requisition_items.transfer_order_item_id =
transfer_order_items.id
JOIN requisition_approval_items ON
requisition_approval_items.requisition_item_id =
transfer_order_item_requisition_items.requisition_item_id
JOIN requisition_approval_item_items ON
requisition_approval_item_items.requisition_approval_item_id =
requisition_approval_items.id
JOIN transfer_orders ON transfer_orders.id =
transfer_order_items.transfer_order_id AND transfer_orders.source_warehouse_id =
warehouse_id
WHERE transfer_orders.created_at >=from_date
AND transfer_orders.created_at <=to_date
UNION ALL
SELECT
transfer_order_items.item_id,
transfer_orders.source_warehouse_id as store_id,
"0" as received_quantity,
"0" as returned_quantity,
"0" as issued_quantity,
"0" as transferred_quantity,
"0" as opening_received_quantity,
"0" as opening_returned_quantity,
"0" as opening_issued_quantity,
requisition_approval_item_items.quantity as opening_transferred_quantity,
transfer_orders.created_at
FROM transfer_order_items
JOIN transfer_order_item_requisition_items ON
transfer_order_item_requisition_items.transfer_order_item_id =
transfer_order_items.id
JOIN requisition_approval_items ON
requisition_approval_items.requisition_item_id =
transfer_order_item_requisition_items.requisition_item_id
JOIN requisition_approval_item_items ON
requisition_approval_item_items.requisition_approval_item_id =
requisition_approval_items.id
JOIN transfer_orders ON transfer_orders.id =
transfer_order_items.transfer_order_id AND transfer_orders.source_warehouse_id =
warehouse_id
WHERE transfer_orders.created_at < from_date
) as c
RIGHT JOIN store_items ON store_items.item_id = c.item_id
JOIN items ON items.id = store_items.item_id
JOIN stores ON stores.id = store_items.store_id
JOIN unit_of_measurements ON unit_of_measurements.id =
items.unit_of_measurement_id
WHERE store_items.store_id = warehouse_id AND store_items.item_id in (
SELECT store_items.item_id FROM store_items WHERE store_items.store_id =
warehouse_id
)
GROUP BY store_items.item_id
ORDER BY c.created_at DESC;
END$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `updateAccountsBalanceAsOf`(IN `as_of`
DATE)
BEGIN
TRUNCATE tempo_account_total_amounts;
INSERT INTO tempo_account_total_amounts
(account_code,account,account_group_code,account_group,account_id,account_group_id,
amount,account_balance)
(
SELECT
accounts.CODE AS account_code,
accounts.NAME AS account,
account_groups.CODE AS account_group_code,
account_groups.NAME AS account_group,
accounts.id AS account_id,
accounts.account_group_id AS account_group_id,
IF
(
find_in_set( accounts.account_group_id,'1,2,3,4,5,10,11')
,
(
(
SELECT

COALESCE (
sum(
IF
(
currencies.is_default,
general_ledgers.amount,
IF
( currencies.action = 'DIVIDE',
general_ledgers.amount / general_ledgers.exchange_rate, general_ledgers.amount *
general_ledgers.exchange_rate )
)
),0
) AS amount
FROM
general_ledgers
JOIN currencies ON currencies.id = general_ledgers.currency_id
WHERE
general_ledgers.type = 'DEBITED'
AND general_ledgers.account_id = accounts.id
AND DATE(general_ledgers.date) <= as_of
)
-
(
SELECT
COALESCE (
sum(
IF
(
currencies.is_default,
general_ledgers.amount,
IF
( currencies.action = 'DIVIDE',
general_ledgers.amount / general_ledgers.exchange_rate, general_ledgers.amount *
general_ledgers.exchange_rate )
)
),0
) AS amount
FROM
general_ledgers JOIN currencies ON currencies.id =
general_ledgers.currency_id
WHERE
general_ledgers.type = 'CREDITED'
AND general_ledgers.account_id = accounts.id
AND DATE(general_ledgers.date) <= as_of
)
),
(
(
SELECT
COALESCE (
sum(
IF
(
currencies.is_default,
general_ledgers.amount,
IF
( currencies.action = 'DIVIDE',
general_ledgers.amount / general_ledgers.exchange_rate, general_ledgers.amount *
general_ledgers.exchange_rate )
)
),0
) AS amount
FROM
general_ledgers JOIN currencies ON currencies.id =
general_ledgers.currency_id
WHERE
general_ledgers.type = 'CREDITED'
AND general_ledgers.account_id = accounts.id
AND DATE(general_ledgers.date) <= as_of
)
-
(
SELECT

COALESCE (
sum(
IF
(
currencies.is_default,
general_ledgers.amount,
IF
( currencies.action = 'DIVIDE',
general_ledgers.amount / general_ledgers.exchange_rate, general_ledgers.amount *
general_ledgers.exchange_rate )
)
),0
) AS amount
FROM
general_ledgers
JOIN currencies ON currencies.id = general_ledgers.currency_id
WHERE
general_ledgers.type = 'DEBITED'
AND general_ledgers.account_id = accounts.id
AND DATE(general_ledgers.date) <= as_of
))
) AS balance,
IF
( find_in_set( accounts.account_group_id,'1,2,3,4,5,10,11'), 'DEBIT',
'CREDIT' ) AS account_balance
FROM
( accounts JOIN account_groups ON ( account_groups.id =
accounts.account_group_id ) )
GROUP BY
accounts.id
);
END$$
DELIMITER ;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `update_recently_requisitions`()
NO SQL
BEGIN
DELETE FROM recently_requisitions;
INSERT INTO recently_requisitions
SELECT
*
FROM
requisitions r
WHERE

r.created_at >= DATE_ADD(


LAST_DAY(
DATE_SUB(NOW(), INTERVAL 2 MONTH)),
INTERVAL 1 DAY
)
;
END$$
DELIMITER ;

You might also like