-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_item.php
119 lines (98 loc) · 3.97 KB
/
update_item.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* Created by PhpStorm.
* User: Ryan
* Date: 11/9/2015
* Time: 2:28 PM
*/
require('db_cn.inc');
require('db_access.inc');
//This file contains php code that will be executed after the
//insert operation is done.
require('update_item_result_ui.inc');
// Main control logic
update_item();
//-------------------------------------------------------------
function update_item()
{
connect_and_select_db(DB_SERVER, DB_UN, DB_PWD,
DB_NAME);
// Get the bannerid and other data sent by the user from the query
$itemNumber = $_REQUEST['itemNumber'];
$itemDescription = mysql_real_escape_string($_POST["itemDescription"]);
$category = mysql_real_escape_string($_POST["category"]);
$deptName = mysql_real_escape_string($_POST["departmentName"]);
$purchCost = mysql_real_escape_string($_POST["purchaseCost"]);
$retail = mysql_real_escape_string($_POST["retailPrice"]);
$updateStmt = "update Item
set ItemDescription = '".$itemDescription."', Category = '".$category."', DepartmentName = '".$deptName."',
PurchaseCost = '".$purchCost."', FullRetailPrice = '".$retail."'".
"WHERE ItemNumber = '".$itemNumber."'";
$result = execute_SQL_query_with_no_error_report($updateStmt);
$message = "";
if (!$result)
{
$message .= "Error in updating Item: ".$itemNumber." in database.<br />".mysql_error()."<hr />";
}
else
{
$message = "Data for Item updated successfully. <br />Item Number: $itemNumber<br />Item Description: $itemDescription
<br />Category: $category <br />Department Name: $deptName<br />Purchase Cost: $purchCost<br
/>Retail Price: $retail<br /><br />";
}
$getPromoItemsStmt = "SELECT * FROM PromotionItem WHERE ItemNumber = '$itemNumber'";
//echo "$getPromoItemsStmt";
$promoItems = execute_SQL_query_with_no_error_report($getPromoItemsStmt);
$numPromoItems = count_rows_in_result_set($promoItems);
while ($promoItem = mysql_fetch_assoc($promoItems))
{
$id = $promoItem['ID'];
$promoCode = $promoItem['PromoCode'];
$oldSalePrice = $promoItem['SalePrice'];
//echo "ID = $id PromoCode: $promoCode OldSalePrice: $oldSalePrice";
$newSalePrice = getNewSalePrice($retail, $promoCode);
$promoItemUpdateStmt = "UPDATE PromotionItem
set SalePrice = '$newSalePrice'
where ID = '$id'";
$result = execute_SQL_query_with_no_error_report($promoItemUpdateStmt);
if (!$result) {
$message .= "Error in updating Promotion Item: ".$id." in database.<br />".mysql_error()."<hr
/>";
} else {
$message .= "Data for Promotion Item with ID: ".$id." updated successfully. <br />Old Sale Price: ".$oldSalePrice."<br />New Sale Price: ".$newSalePrice."<hr />";
}
}
ui_show_item_update_details($message);
}
function getNewSalePrice($item_retail_price, $promoCode)
{
$promoType = getPromoType($promoCode);
$amountOff = getAmountOff($promoCode);
//echo "PromoType: $promoType\nAmount off: $amountOff\nRetail Price: $item_retail_price\n";
if($promoType == "Percent")
{
$result = ($item_retail_price - ($amountOff * $item_retail_price));
//echo "Percent result: $result";
}
else{
$result = ($item_retail_price - $amountOff);
//echo "Dollar result: $result";
}
$result = round($result, 2);
return $result;
}
function getPromoType($pCode)
{
$select_stmt = "SELECT PromoType FROM Promotion WHERE PromoCode = $pCode";
$not_found_message = "Could not retrieve Promotion Type from Promotion.";
$result = get_unique_row($select_stmt, $not_found_message, $not_found_message);
return $result['PromoType'];
}
function getAmountOff($pCode)
{
$select_stmt = "SELECT AmountOff FROM Promotion WHERE PromoCode = $pCode";
$not_found_message = "Could not retrieve Amount Off from Promotion.";
$result = get_unique_row($select_stmt, $not_found_message, $not_found_message);
return $result['AmountOff'];
}
?>