Explorar el Código

fix: purchase master

Muhammad Iqbal Afandi hace 3 años
padre
commit
d24c930895

+ 42
- 17
app/Http/Controllers/PurchaseController.php Ver fichero

84
         DB::beginTransaction();
84
         DB::beginTransaction();
85
 
85
 
86
         try {
86
         try {
87
-            $ppn = Ppn::first()->ppn;
88
-
89
             $validated = $request
87
             $validated = $request
90
                 ->safe()
88
                 ->safe()
91
                 ->merge([
89
                 ->merge([
92
                     "user_id" => auth()->user()->id,
90
                     "user_id" => auth()->user()->id,
93
-                    "ppn" => $request->ppn ? $ppn : 0,
91
+                    "ppn" => $request->ppn ? true : false,
94
                 ])
92
                 ])
95
                 ->all();
93
                 ->all();
96
 
94
 
190
         DB::beginTransaction();
188
         DB::beginTransaction();
191
 
189
 
192
         try {
190
         try {
193
-            $ppn = Ppn::first()->ppn;
194
-
195
             $validated = $request
191
             $validated = $request
196
                 ->safe()
192
                 ->safe()
197
                 ->merge([
193
                 ->merge([
198
-                    "ppn" => $request->ppn ? $ppn : 0,
194
+                    "ppn" => $request->ppn ? true : false,
199
                 ])
195
                 ])
200
                 ->all();
196
                 ->all();
201
 
197
 
227
                 }
223
                 }
228
 
224
 
229
                 if ($request->status == "success") {
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
             DB::commit();
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
         } catch (QueryException $e) {
272
         } catch (QueryException $e) {
248
             DB::rollBack();
273
             DB::rollBack();
249
 
274
 

+ 1
- 18
app/Models/StockProduct.php Ver fichero

2
 
2
 
3
 namespace App\Models;
3
 namespace App\Models;
4
 
4
 
5
-use App\Services\HelperService;
6
-use Illuminate\Database\Eloquent\Casts\Attribute;
7
 use Illuminate\Database\Eloquent\Factories\HasFactory;
5
 use Illuminate\Database\Eloquent\Factories\HasFactory;
8
 use Illuminate\Database\Eloquent\Model;
6
 use Illuminate\Database\Eloquent\Model;
9
 
7
 
11
 {
9
 {
12
     use HasFactory;
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
     public function product()
14
     public function product()
32
     {
15
     {

+ 1
- 9
database/migrations/2022_06_16_092657_create_stock_products_table.php Ver fichero

13
     public function up()
13
     public function up()
14
     {
14
     {
15
         Schema::create("stock_products", function (Blueprint $table) {
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
             $table->unsignedInteger("price");
16
             $table->unsignedInteger("price");
26
             $table->integer("qty");
17
             $table->integer("qty");
18
+            $table->unsignedTinyInteger("ppn");
27
             $table->string("product_number");
19
             $table->string("product_number");
28
             $table
20
             $table
29
                 ->foreign("product_number")
21
                 ->foreign("product_number")

+ 2
- 2
resources/js/pages/Purchases/Create.vue Ver fichero

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