Bladeren bron

fix: purchase master

Muhammad Iqbal Afandi 3 jaren geleden
bovenliggende
commit
d24c930895

+ 42
- 17
app/Http/Controllers/PurchaseController.php Bestand weergeven

@@ -84,13 +84,11 @@ class PurchaseController extends Controller
84 84
         DB::beginTransaction();
85 85
 
86 86
         try {
87
-            $ppn = Ppn::first()->ppn;
88
-
89 87
             $validated = $request
90 88
                 ->safe()
91 89
                 ->merge([
92 90
                     "user_id" => auth()->user()->id,
93
-                    "ppn" => $request->ppn ? $ppn : 0,
91
+                    "ppn" => $request->ppn ? true : false,
94 92
                 ])
95 93
                 ->all();
96 94
 
@@ -190,12 +188,10 @@ class PurchaseController extends Controller
190 188
         DB::beginTransaction();
191 189
 
192 190
         try {
193
-            $ppn = Ppn::first()->ppn;
194
-
195 191
             $validated = $request
196 192
                 ->safe()
197 193
                 ->merge([
198
-                    "ppn" => $request->ppn ? $ppn : 0,
194
+                    "ppn" => $request->ppn ? true : false,
199 195
                 ])
200 196
                 ->all();
201 197
 
@@ -227,23 +223,52 @@ class PurchaseController extends Controller
227 223
                 }
228 224
 
229 225
                 if ($request->status == "success") {
230
-                    $validated = [
231
-                        "purchase_number" => $purchase->number,
232
-                        "price" => $product["price"],
233
-                        "qty" => $product["qty"],
234
-                        "product_number" => $product["number"],
235
-                    ];
226
+                    $stockProduct = StockProduct::where(
227
+                        "product_number",
228
+                        $product["number"]
229
+                    );
230
+
231
+                    if ($stockProduct->exists()) {
232
+                        $validated = [
233
+                            "price" =>
234
+                                $stockProduct->first()->price >
235
+                                $product["price"]
236
+                                    ? $stockProduct->first()->price
237
+                                    : $product["price"],
238
+                            "ppn" => $request->ppn ? true : false,
239
+                        ];
236 240
 
237
-                    StockProduct::create($validated);
241
+                        $stockProduct->increment(
242
+                            "qty",
243
+                            $product["qty"],
244
+                            $validated
245
+                        );
246
+                    } else {
247
+                        $validated = [
248
+                            "price" => $product["price"],
249
+                            "ppn" => $request->ppn ? true : false,
250
+                            "qty" => $product["qty"],
251
+                            "product_number" => $product["number"],
252
+                        ];
253
+
254
+                        StockProduct::create($validated);
255
+                    }
238 256
                 }
239 257
             }
240 258
 
241 259
             DB::commit();
242 260
 
243
-            return back()->with(
244
-                "success",
245
-                __("messages.success.update.purchase")
246
-            );
261
+            if ($request->status == "success") {
262
+                return to_route("purchases.show", $purchase)->with(
263
+                    "success",
264
+                    __("messages.success.update.purchase")
265
+                );
266
+            } else {
267
+                return back()->with(
268
+                    "success",
269
+                    __("messages.success.update.purchase")
270
+                );
271
+            }
247 272
         } catch (QueryException $e) {
248 273
             DB::rollBack();
249 274
 

+ 1
- 18
app/Models/StockProduct.php Bestand weergeven

@@ -2,8 +2,6 @@
2 2
 
3 3
 namespace App\Models;
4 4
 
5
-use App\Services\HelperService;
6
-use Illuminate\Database\Eloquent\Casts\Attribute;
7 5
 use Illuminate\Database\Eloquent\Factories\HasFactory;
8 6
 use Illuminate\Database\Eloquent\Model;
9 7
 
@@ -11,22 +9,7 @@ class StockProduct extends Model
11 9
 {
12 10
     use HasFactory;
13 11
 
14
-    protected $fillable = [
15
-        "purchase_number",
16
-        "sale_number",
17
-        "price",
18
-        "qty",
19
-        "product_number",
20
-    ];
21
-
22
-    protected function price(): Attribute
23
-    {
24
-        $ppn = Purchase::where("number", $this->purchase_number)->first()->ppn;
25
-
26
-        return Attribute::make(
27
-            get: fn($value) => HelperService::addPPN($value, $ppn)
28
-        );
29
-    }
12
+    protected $fillable = ["price", "qty", "ppn", "product_number"];
30 13
 
31 14
     public function product()
32 15
     {

+ 1
- 9
database/migrations/2022_06_16_092657_create_stock_products_table.php Bestand weergeven

@@ -13,17 +13,9 @@ return new class extends Migration {
13 13
     public function up()
14 14
     {
15 15
         Schema::create("stock_products", function (Blueprint $table) {
16
-            $table->id();
17
-            $table
18
-                ->string("purchase_number")
19
-                ->nullable()
20
-                ->default(null);
21
-            $table
22
-                ->string("sale_number")
23
-                ->nullable()
24
-                ->default(null);
25 16
             $table->unsignedInteger("price");
26 17
             $table->integer("qty");
18
+            $table->unsignedTinyInteger("ppn");
27 19
             $table->string("product_number");
28 20
             $table
29 21
                 ->foreign("product_number")

+ 2
- 2
resources/js/pages/Purchases/Create.vue Bestand weergeven

@@ -49,7 +49,7 @@ const onSubmit = () => {
49 49
       onSuccess: () => {
50 50
         form.reset()
51 51
 
52
-        onClearProduct()
52
+        onClearProductCart()
53 53
       },
54 54
     })
55 55
 }
@@ -60,10 +60,10 @@ const dropdownStatus = computed(() => {
60 60
 
61 61
 const {
62 62
   productCart,
63
+  onClearProductCart,
63 64
   onAddProduct,
64 65
   onDeleteProduct,
65 66
   onEditProduct,
66
-  onClearProduct,
67 67
   totalProductPrice,
68 68
 } = useProductCart(form)
69 69