Browse Source

added stock edition

keplyx 4 years ago
parent
commit
d935a40bcb

+ 6
- 0
admin/articles.php View File

1
+<?php
2
+$relativePath = "../";
3
+
4
+$pageTitle = "Gestion des articles";
5
+$script = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/articles.js\"></script>";
6
+include("template.php");

+ 17
- 7
admin/index.php View File

2
 $relativePath = "../";
2
 $relativePath = "../";
3
 ob_start();
3
 ob_start();
4
 ?>
4
 ?>
5
-<div class="admin-container">
6
-    <a href="stock.php">
7
-        <button class="btn btn-primary btn-large">Gestion des stocks</button>
8
-    </a>
9
-    <a href="categories.php">
10
-        <button class="btn btn-primary btn-large">Gestion des catégories</button>
11
-    </a>
5
+<div class="admin-container" style="display: flex">
6
+    <div style="margin: auto">
7
+        <div style="display: flex" class="my-5">
8
+            <a href="stock.php" style="margin: auto">
9
+                <button class="btn btn-primary btn-lg">Gestion du stocks</button>
10
+            </a>
11
+        </div>
12
+
13
+        <div class="my-5" style="display: flex">
14
+            <a href="articles.php" style="margin: auto" class="mx-1">
15
+                <button class="btn btn-primary btn-lg">Créer/Éditer des articles</button>
16
+            </a>
17
+            <a href="categories.php" style="margin: auto" class="mx-1">
18
+                <button class="btn btn-primary btn-lg">Créer/Éditer des catégories</button>
19
+            </a>
20
+        </div>
21
+    </div>
12
 </div>
22
 </div>
13
 
23
 
14
 <link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>
24
 <link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>

+ 3
- 2
admin/stock.php View File

1
 <?php
1
 <?php
2
 $relativePath = "../";
2
 $relativePath = "../";
3
 
3
 
4
-$pageTitle = "Gestion des articles";
5
-$script = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/articles.js\"></script>";
4
+$isStock = true;
5
+$pageTitle = "Gestion des stocks";
6
+$script = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/stock.js\"></script>";
6
 include("template.php");
7
 include("template.php");

+ 38
- 12
admin/template.php View File

1
 <?php
1
 <?php
2
 $relativePath = "../";
2
 $relativePath = "../";
3
 
3
 
4
+if (!isset($isStock))
5
+    $isStock = false;
6
+
4
 ob_start();
7
 ob_start();
5
 ?>
8
 ?>
6
 <div class="admin-container">
9
 <div class="admin-container">
7
 
10
 
8
     <a href="index.php">
11
     <a href="index.php">
9
-        <button class="btn btn-primary btn-large">Retour</button>
12
+        <button class="btn btn-primary btn-large">
13
+            <span class="mdi mdi-chevron-left"></span>
14
+            Retour
15
+        </button>
10
     </a>
16
     </a>
11
 
17
 
12
     <h1 class="text-center"><?= $pageTitle ?></h1>
18
     <h1 class="text-center"><?= $pageTitle ?></h1>
13
 
19
 
14
-    <div style="display: flex">
15
-        <button class="btn btn-success" style="margin: auto" onclick="listManager.showEditPopup(-1)">
16
-            <i class="mdi mdi-plus"></i>
17
-        </button>
18
-    </div>
20
+    <?php if (!$isStock): ?>
21
+        <div style="display: flex">
22
+            <button class="btn btn-success" style="margin: auto" onclick="listManager.showEditPopup(-1)">
23
+                <i class="mdi mdi-plus"></i>
24
+            </button>
25
+        </div>
26
+        <div id="listContainer">
27
+            <ul id="dataList">
19
 
28
 
20
-    <div id="listContainer">
21
-        <ul id="dataList">
29
+            </ul>
30
+        </div>
31
+    <?php else: ?>
32
+        <div>
33
+            <h3 class="text-center">Mode</h3>
34
+            <div class="button-container">
35
+                <button id="buyButton" class="btn mr-0">Achat</button>
36
+                <button id="sellButton" class="btn ml-0">Vente</button>
37
+            </div>
38
+        </div>
39
+        <div id="dataList">
22
 
40
 
23
-        </ul>
24
-    </div>
41
+        </div>
42
+        <div style="display: flex">
43
+            <button id="saveButton" style="margin: auto" class="btn btn-lg my-2">Sauvegarder</button>
44
+        </div>
45
+    <?php endif; ?>
25
 
46
 
26
 </div>
47
 </div>
27
 
48
 
28
 
49
 
29
-<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/libs/materialdesignicons.min.css" media="screen,projection"/>
50
+<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/libs/materialdesignicons.min.css"
51
+      media="screen,projection"/>
30
 <link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>
52
 <link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>
31
-<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/categories.css" media="screen,projection"/>
32
 
53
 
54
+<?php if (!$isStock): ?>
55
+    <link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/list.css" media="screen,projection"/>
56
+<?php else: ?>
57
+    <link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/stock.css" media="screen,projection"/>
58
+<?php endif; ?>
33
 
59
 
34
 <?php
60
 <?php
35
 $pageContent = ob_get_clean();
61
 $pageContent = ob_get_clean();

assets/css/categories.css → assets/css/list.css View File

33
     margin: 20px 0 20px 0;
33
     margin: 20px 0 20px 0;
34
 }
34
 }
35
 
35
 
36
-.mdi {
36
+#listContainer .mdi {
37
     font-size: 2rem;
37
     font-size: 2rem;
38
 }
38
 }
39
 
39
 
40
 .list-name {
40
 .list-name {
41
     font-weight: bold;
41
     font-weight: bold;
42
-    width: 150px;
42
+    width: 200px;
43
     height: 50px;
43
     height: 50px;
44
     overflow: hidden;
44
     overflow: hidden;
45
 }
45
 }
61
     text-align: right;
61
     text-align: right;
62
 }
62
 }
63
 
63
 
64
-.list-image img  {
64
+.list-image  {
65
     width: 50px;
65
     width: 50px;
66
     height: 50px;
66
     height: 50px;
67
+    display: flex;
68
+}
69
+
70
+
71
+.list-image img {
72
+    width: 45px;
73
+    height: 45px;
74
+    margin: auto;
67
 }
75
 }

+ 27
- 17
assets/css/stock.css View File

1
+#dataList {
2
+    display: grid;
3
+    grid-template-columns: auto auto ;
4
+    width: 100%;
5
+}
1
 
6
 
2
-.name-column {
3
-    width: 200px;
7
+.img-container {
8
+    display: flex;
9
+    width: 100%;
4
 }
10
 }
5
 
11
 
6
-.quantity-column, .price-column {
7
-    width: 150px;
12
+.grid-item {
13
+    display: flex;
14
+    padding: 5px;
8
 }
15
 }
9
 
16
 
10
-.code-column {
11
-    width: 200px;
17
+
18
+.grid-container {
19
+    width: 100px;
20
+    margin: auto;
21
+    display: block;
12
 }
22
 }
13
 
23
 
14
-.type-column {
15
-    width: 200px;
16
-    overflow-x: auto;
24
+.grid-container input {
25
+    width: 100px;
17
 }
26
 }
18
 
27
 
19
-.image-column {
20
-    width: 150px;
28
+.grid-container p {
29
+    text-align: center;
21
 }
30
 }
22
 
31
 
23
-.actions-column {
24
-    width: 100px;
32
+.grid-container img {
33
+    width : 100px;
34
+    margin: auto;
25
 }
35
 }
26
 
36
 
27
-#stockTable tr {
28
-    border-bottom: 1px solid #dedede;
37
+.button-container {
38
+    display: flex;
29
 }
39
 }
30
 
40
 
31
-.row-img {
32
-    height: 50px;
41
+.button-container button {
42
+    margin: auto;
33
 }
43
 }

+ 17
- 0
assets/js/ajaxManager.js View File

78
         } else
78
         } else
79
             return 1;
79
             return 1;
80
     }
80
     }
81
+
82
+    static async saveStockChange(data, isSell) {
83
+        let formattedData = {
84
+            type: 'stock',
85
+            action: isSell ? 'sell' : 'buy',
86
+            data: data,
87
+        };
88
+        let response = await $.ajax({
89
+            type: "POST",
90
+            url: "json_ajax.php",
91
+            data: JSON.stringify(formattedData),
92
+            dataType: "json",
93
+            contentType: "application/json; charset=utf-8",
94
+        });
95
+        console.log(response);
96
+        return response['data'];
97
+    }
81
 }
98
 }
82
 
99
 
83
 
100
 

+ 1
- 1
assets/js/jquery-confirm-defaults.js View File

1
 jconfirm.defaults = {
1
 jconfirm.defaults = {
2
     title: 'Title',
2
     title: 'Title',
3
     titleClass: '',
3
     titleClass: '',
4
-    type: 'red',
4
+    type: 'blue',
5
     typeAnimated: true,
5
     typeAnimated: true,
6
     draggable: false,
6
     draggable: false,
7
     dragWindowGap: 15,
7
     dragWindowGap: 15,

+ 3
- 1
assets/js/listManager.js View File

138
     }
138
     }
139
 
139
 
140
     getImagePicker(id, index) {
140
     getImagePicker(id, index) {
141
-        let imageSrc = $(this.displayedData[index].find("img")[0]).attr('src');
141
+        let imageSrc = undefined;
142
+        if (index !== -1)
143
+            imageSrc = $(this.displayedData[index].find("img")[0]).attr('src');
142
         return (
144
         return (
143
             '<img style="width: 50px; height: 50px" src="' + imageSrc + '" id="image_' + id + '"/>' +
145
             '<img style="width: 50px; height: 50px" src="' + imageSrc + '" id="image_' + id + '"/>' +
144
             '<div class="custom-file">\n' +
146
             '<div class="custom-file">\n' +

+ 106
- 0
assets/js/stock.js View File

1
+let listContainer = $("#dataList");
2
+let displayedItems = [];
3
+let fetchedData = {};
4
+
5
+let currentMode = "sell";
6
+
7
+let sellCLass = 'btn-danger';
8
+let buyClass = 'btn-success';
9
+
10
+$(document).ready(async function () {
11
+    fetchedData = await AjaxManager.getAll();
12
+
13
+    $('#sellButton').addClass(sellCLass);
14
+    $('#saveButton').addClass(sellCLass);
15
+
16
+    generateList();
17
+    $('#buyButton').on('click', function () {
18
+        if (!$(this).hasClass(buyClass)) {
19
+            $(this).addClass(buyClass);
20
+            $('#sellButton').removeClass(sellCLass);
21
+            $('#saveButton').removeClass(sellCLass).addClass(buyClass);
22
+            currentMode = "buy";
23
+        }
24
+    });
25
+    $('#sellButton').on('click', function () {
26
+        if (!$(this).hasClass(sellCLass)) {
27
+            $(this).addClass(sellCLass);
28
+            $('#buyButton').removeClass(buyClass);
29
+            $('#saveButton').removeClass(buyClass).addClass(sellCLass);
30
+            currentMode = "sell";
31
+        }
32
+    });
33
+    $('#saveButton').on('click', function () {
34
+        let values = getValues();
35
+        let message = 'Voulez vous vraimer ajouter ces articles au stock ?<br><ul>';
36
+        let color = 'green';
37
+        if (currentMode === "sell"){
38
+            message = 'Voulez vous vraiment supprimer ces articles du stock ?<br><ul>';
39
+            color = 'red';
40
+        }
41
+        for (let  i = 0; i < values.length; i++) {
42
+            if (values[i]['value'] > 0){
43
+                message += '<li>' + fetchedData['articles'][i]['name'] + ' : ' + values[i]['value'] + '</li>'
44
+            }
45
+        }
46
+        message += '</ul>';
47
+        $.confirm({
48
+            title: 'Confirmer',
49
+            content: message,
50
+            type: color,
51
+            buttons: {
52
+                formSubmit: {
53
+                    text: 'Confirmer',
54
+                    action: function () {
55
+                        window.location.reload();
56
+                    }
57
+                },
58
+                cancel: {
59
+                    text: 'Annuler',
60
+                }
61
+            }
62
+        });
63
+        AjaxManager.saveStockChange(getFormattedValues(values), currentMode === "sell");
64
+    });
65
+});
66
+
67
+function getListItem(item) {
68
+    return (
69
+        '<div class="grid-item">' +
70
+        '<div class="grid-container">' +
71
+        '<img class="list-image" src="../uploaded_images/' + item['id'] + '.jpg"/>' +
72
+        '<p>' + item['name'] + '</p>' +
73
+        '<p>Quantité : ' + item['quantity'] + '</p>' +
74
+        '<input placeholder="Nombre" type="number" value="0" class="form-control"/>' +
75
+        '</div>' +
76
+        '</div>');
77
+}
78
+
79
+
80
+function generateList() {
81
+    for (let i = 0; i < fetchedData['articles'].length; i++) {
82
+        let listItem = getListItem(fetchedData['articles'][i]);
83
+        displayedItems.push($(listItem));
84
+        listContainer.append(displayedItems[i]);
85
+    }
86
+}
87
+
88
+
89
+function getValues() {
90
+    let values = [];
91
+    for (let  i = 0; i < displayedItems.length; i++) {
92
+        let value = displayedItems[i].find('input').val();
93
+        let id = fetchedData['articles'][i]['id'];
94
+        values.push({id: id, value: value});
95
+    }
96
+    return values;
97
+}
98
+
99
+function getFormattedValues(values) {
100
+    let newValues = [];
101
+    for (let  i = 0; i < values.length; i++) {
102
+        if (values[i]['value'] > 0)
103
+            newValues.push(values[i]);
104
+    }
105
+    return newValues;
106
+}

+ 8
- 0
classes/dao.php View File

137
         } else
137
         } else
138
             return 0;
138
             return 0;
139
     }
139
     }
140
+
141
+    public function update_article_stock($articleid, $diff)
142
+    {
143
+        $sql = 'UPDATE articles SET quantity=quantity+? WHERE id=?';
144
+        $cursor = $this->conn->prepare($sql);
145
+        $data = [$diff, $articleid];
146
+        return $cursor->execute($data);
147
+    }
140
 }
148
 }

+ 17
- 2
classes/postHandler.php View File

3
 
3
 
4
 class PostHandler
4
 class PostHandler
5
 {
5
 {
6
-    private $valid_types = ["article", "category", "article_categories", 'image'];
7
-    private $valid_actions = ["create", "update", "remove", "get"];
6
+    private $valid_types = ["article", "category", "article_categories", "image", "stock"];
7
+    private $valid_actions = ["create", "update", "remove", "get", "buy", "sell"];
8
 
8
 
9
     private $action;
9
     private $action;
10
     private $type;
10
     private $type;
35
         $result = -1;
35
         $result = -1;
36
         if ($this->type == "image") {
36
         if ($this->type == "image") {
37
             $result = $this->save_image();
37
             $result = $this->save_image();
38
+        } else if ($this->type == "stock") {
39
+            $result = $this->updateStock();
38
         } else if (count($this->data) > 0) {
40
         } else if (count($this->data) > 0) {
39
             if ($this->action == "create")
41
             if ($this->action == "create")
40
                 $result = $this->create();
42
                 $result = $this->create();
151
         return $result;
153
         return $result;
152
     }
154
     }
153
 
155
 
156
+    function updateStock() {
157
+        $result = 0;
158
+        foreach ($this->data as $row) {
159
+            $value = $row["value"];
160
+            if ($this->action == "sell")
161
+                $value = -$value;
162
+            $result = $this->dao->update_article_stock($row["id"], $value);
163
+            if (!$result)
164
+                break;
165
+        }
166
+        return $result;
167
+    }
168
+
154
     function setUnknownTypeResponse()
169
     function setUnknownTypeResponse()
155
     {
170
     {
156
         $this->responseArray["status"] = 1;
171
         $this->responseArray["status"] = 1;

Loading…
Cancel
Save