function apply_discount($user_id, $tour_id) { $tw_user = tw_users::record_by_id($user_id); $tw_tour = tw_tours::record_by_id($tour_id); $discount = new tw_discount(); $prs = array(); if ($tw_user->userCompletedTours >= 1) { $prs['retcust'] = $discount->find_records(array('AutoKey' => 'retcust1'))->first(); } if ($tw_user->userType == 'guard') { $prs['retcust'] = $discount->find_records(array('AutoKey' => 'retcust2'))->first(); } $friends = new projectfriends(); $friends->find_records(array('User_id' => $tw_user->get_id())); if ($friends->count() > 0) { $prs['projectfriends'] = $discount->find_records(array('AutoKey' => 'projectfriends'))->first(); } foreach ($prs as $ppr) { $pr = record_by_id($ppr->get_id(), 'tw_discount'); $pm = new tw_payments(); $pm->find_records(array('tourID' => $tour_id, 'userID' => $user_id, 'Discount_id' => $pr->get_id())); if ($pm->count() > 0) { return; } $r = $pm->new_record(); $r->Discount_id = $pr->get_id(); $r->Title = "{$pr->Type} {$pr->Title}"; $r->Amount = $pr->Amount; if ($pr->AmountType == '%') { $r->Amount = round($pr->Amount * 0.01 * $tw_tour->tourPrice1); } $r->Type = "скидка"; $r->Hidden = 0; $r->tourID = $tw_tour->get_id(); $r->userID = $tw_user->get_id(); $pm->commit(); } }