Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.2 kB
2
Indexable
Never
    public function chargeUpdate($request)
    {
        DB::beginTransaction();
        try {
            $charge = '';
            foreach ($request->weights as $key => $weight) {
                if (!$request->cod_ids[$key]) {
                    $charge                 = new Charge;
                    $charge->weight         = $weight;
                    $charge->same_day       = $request->same_day[$key];
                    $charge->next_day       = $request->next_day[$key];
                    $charge->sub_city       = $request->sub_city[$key];
                    $charge->sub_urban_area = $request->sub_urban_area[$key];
                    $charge->save();
                }
                if (!empty($weight) && !empty($request->cod_ids[$key])) {
                    $merchants  = Merchant::all();
                    $charges    = Charge::all();
                    foreach($merchants as $merchant) {
                        $chargeData = $merchant->charges;
                        foreach ($request->weights as $k => $w) {
                            $same_day_diff       = $request->same_day[$k] - $chargeData[$w]['same_day'];
                            $next_day_diff       = $request->next_day[$k] - $chargeData[$w]['next_day'];
                            $sub_city_diff       = $request->sub_city[$k] - $chargeData[$w]['sub_city'];
                            $sub_urban_area_diff = $request->sub_urban_area[$k] - $chargeData[$w]['sub_urban_area'];
                            $same_day            = $chargeData[$w]['same_day'];
                            $next_day            = $chargeData[$w]['next_day'];

                            $sub_city           = $chargeData[$w]['sub_city'];
                            $sub_urban_area     = $chargeData[$w]['sub_urban_area'];

                            if (isset($chargeData[$w])) {
                                $chargeData[$w]['same_day']       = $same_day + $same_day_diff;
                                $chargeData[$w]['next_day']       = $next_day + $next_day_diff;
                                $chargeData[$w]['sub_city']       = $sub_city + $sub_city_diff;
                                $chargeData[$w]['sub_urban_area'] = $sub_urban_area + $sub_urban_area_diff;
                            }
                        }
                        $merchant->charges = $chargeData;
                        $merchant->save();
                    }
                }
                $charge                 = Charge::find($request->cod_ids[$key]);
                $charge->weight         = $weight;
                $charge->same_day       = $request->same_day[$key];
                $charge->next_day       = $request->next_day[$key];
                $charge->sub_city       = $request->sub_city[$key];
                $charge->sub_urban_area = $request->sub_urban_area[$key];
                $charge->save();
            }

            DB::commit();
            return true;
        } catch (\Exception $e) {
            dd($e->getMessage());
            DB::rollback();
            return false;
        }
    }
Leave a Comment