Leave duration Laravel : BusinessTime between two dates - laravel-9

I have a leave management project in laravel 9. I want to create a function(Helper) to get the total duration of the leave but counting only the business time (08:00 - 17:00) and work day (no weekend and holydays).
I defined my businessTime with the cmixin/business-time package.
BusinessTime::enable(Carbon::class, [
'monday' => ['08:00-12:00', '13:00-17:00'],
'tuesday' => ['08:00-12:00', '13:00-17:00'],
'wednesday' => ['08:00-12:00'],
'thursday' => ['08:00-12:00', '13:00-17:00'],
'friday' => ['08:00-12:00', '13:00-17:00'],
'saturday' => [],
'sunday' => [],
'exceptions' => [
'01-01' => [], // Recurring on each 1st of january
'12-25' => ['09:00-12:00'], // Recurring on each 25th of december
],
]);
But Carbon diff() and date_diff() take the whole duration.
Thank you.

Related

Get an array of hashes corresponding to the selected values with ActiveRecord

I have a sql database that I query with ActiveRecord. (version of ActiveRecord is 4.2.5.1)
My query look like this:
User.
select('COUNT (DISTINCT user_id) as user_count').
select("date_trunc('month', created_at) as month").
select('city').
where('created_at > ?', 1.year.ago.utc.to_s(:db)).
group('month, city').
to_a.map do |row|
{
user_count: row.user_count,
month: row.month,
city: row.city
}
And I get results like this:
[
[ 0] {
:user_count => 165,
:month => 2015-09-01 00:00:00 UTC,
:city => "Paris"
},
[ 1] {
:user_count => 33,
:month => 2015-09-01 00:00:00 UTC,
:city => "Berlin"
},
...
]
And this is fine.
However, with this query:
User.
select('COUNT (DISTINCT user_id) as user_count').
select("date_trunc('month', created_at) as month").
select('city').
where('created_at > ?', 1.year.ago.utc.to_s(:db)).
group('month, city').
to_a
I get results like this:
[
[ 0] #<User:0x007facebac3560> {
:user_id => nil, # wtf is this
:city => "Paris"
# where is the month?
},
[ 1] #<User:0x007facebac33a8> {
:user_id => nil,
:city => "Berlin"
},
...
]
Which is not fine.
Why do I have to manually map the results even when I selected the rows I wanted?
Ideally, what I want is this:
User.
select(...).
where(...).
group(...).
to_array_of_hashes_with_the_keys_i_selected
Notice that if my query is a custom query, to_a does what I want, for example:
ActiveRecord::Base.connection.execute(<<~SQL
SELECT COUNT (DISTINCT user_id) as user_count, date_trunc('month', created_at) as month, city
FROM users
WHERE created_at > '2015-09-01 13:28:40'
GROUP BY month, city
SQL
).to_a # this is fine
From How to convert activerecord results into a array of hashes, I can do:
User.
select(...).
where(...).
group(...).
as_json
And I get
[
[ 0] {
"user_id" => nil,
"city" => "Paris",
"user_count" => 165,
"month" => 2015-09-01 00:00:00 UTC
},
...
Not exactly what I selected, but close enough.
.as_json.map{|e|e.except('user_id')} gives me what I want

how to display datepicker upto current date and should not display future date in yii framework

i am developing web application by using yii framework, i am displaying datepicker for filing date of birth, i have two dropdown also in my datepicker like month and year now everything is working fine. this is my datepicker with month and year dropdown code
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'attribute' => 'birth',
'name' => 'birth',
'value'=>$model->birth,
'model' => $model,
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-m-d',
'changeYear' => true, // can change year
'changeMonth' => true, // can change month
'yearRange' => '1950:2099', // range of year
'minDate' => '1950-01-01', // minimum date
'maxDate' => '2099-12-31',
'showButtonPanel' => false,
),
'htmlOptions' => array(
'size' => '10', // textField size
'maxlength' => '10', // textField maxlength
),
));
?>
my requirement:-
actually i am using datepicker for date of birth so, i need to display calender upto current date. for example today date is 5/4/2015 and my calender also should show upto 5/4/2015 which means it should not display future date. is it possible?. if it is possible how can we do it. could you please help me
Just set
'yearRange' => '1950:'.date('Y'),
'maxDate' => date('Y-m-d')

Add multiple products to paypal express checkout

For a few hours i'm trying to list into paypal express checkout multiple products. This has to be done in order to increase the customers trust for what they buy.
How i can create the bellow array in order to be reconized by paypal as multiple products?
Listing of 1 product is not a problem. Here is the code:
$requestParams = array(
'RETURNURL' => '***',
'CANCELURL' => '***'
);
$item = array('L_PAYMENTREQUEST_0_NAME0' => 'Test product ',
'L_PAYMENTREQUEST_0_DESC0' => 'Description of my item',
'L_PAYMENTREQUEST_0_AMT0' => '0.01',
'L_PAYMENTREQUEST_0_QTY0' => '1'
);
$orderParams = array(
'PAYMENTREQUEST_0_AMT' => '0.01',
'PAYMENTREQUEST_0_CURRENCYCODE' => 'USD',
'PAYMENTREQUEST_0_ITEMAMT' => '0.01',
'PAYMENTREQUEST_0_SHIPPINGAMT' => '0'
);
$response = $core->paypal->request('SetExpressCheckout',$requestParams + $item + $orderParams);
I have tried lots of combinations like adding keys and values into $item array like that in order to add more products to be listed:
I tried also to add in a similar way keys to $orderParams array but without success.
Either i got errors from paypal api, either the paypal listed only the first product.
$item = array('L_PAYMENTREQUEST_0_NAME0' => 'Test product ',
'L_PAYMENTREQUEST_0_DESC0' => 'Description of my item',
'L_PAYMENTREQUEST_0_AMT0' => '0.01',
'L_PAYMENTREQUEST_0_QTY0' => '1',
'L_PAYMENTREQUEST_1_NAME1' => 'Test product 1',
'L_PAYMENTREQUEST_1_DESC1' => 'Description of my next item',
'L_PAYMENTREQUEST_1_AMT1' => '0.01',
'L_PAYMENTREQUEST_1_QTY1' => '1'
);
This is my first integration, i understand the paypal flow but i can't go over this.
Thanks.
Ok, it was a simple trick to do. For those who might need it:
L_PAYMENTREQUEST_n_NAMEm - "n" is the number of transaction, 0 for 1 single transaction - "m" is the number of the product
$item = array('L_PAYMENTREQUEST_0_NAME0' => 'Test product ', //title of the first product
'L_PAYMENTREQUEST_0_DESC0' => 'Description of my item', //description of the forst product
'L_PAYMENTREQUEST_0_AMT0' => '0.01', //amount first product
'L_PAYMENTREQUEST_0_QTY0' => '1', //qty first product
'L_PAYMENTREQUEST_0_NAME1' => 'Test ', // title of the second product
'L_PAYMENTREQUEST_0_DESC1' => 'Description item',//description of the second product
'L_PAYMENTREQUEST_0_AMT1' => '0.01',//amount second product
'L_PAYMENTREQUEST_0_QTY1' => '1'//qty second product
);
$orderParams = array(
'PAYMENTREQUEST_0_PAYMENTACTION'=>'Sale', //becouse we want to sale something
'PAYMENTREQUEST_0_AMT' => '0.02', //total amount (items amount+shipping..etc)
'PAYMENTREQUEST_0_CURRENCYCODE' => 'USD', //curency code
'PAYMENTREQUEST_0_ITEMAMT' => '0.02', //total amount items, without shipping and other taxes
'PAYMENTREQUEST_0_SHIPPINGAMT' => '0' //the shipping amount, will be 0 coz we sell digital products
);
Above you can see an example for two products.
These keys and values will be send to express checkout api in order to deliver the token.
The vars will be sent with GET.

Rails date select assigns month a value when blank

I am using Rails date_select as follows:
<%= date_select :user, :birthday, {:start_year => Time.now.year, :end_year => 1910,:order => [:day,:month,:year], :prompt => { :day => 'day', :month => 'month', :year => 'year' }}, { :class => "default" } %>
When a user forgets to enter a value for the day or month and submit the form when the information is put into the model an error is generated. However the day and month fields also get assigned to 1. This results in the form showing 1 for the day field and January for the month field.
How can I stop this from happening?

Change date_select values from english to spanish in rails

I am building a site in rails and I have a date selector with a drop down menu that Rails generate automatically. The problem is that my site is in spanish and the values of the dropdown for the months are in English, is there a way to change the language to spanish?
I tried adding some lines of codes to the config/environment.rb that I found here
The code is basically this:
require 'date'
class Date
MONTHNAMES = [nil] + %w(Enero Febrero Marzo Abril Mayo Junio Julio Agosto Septiembre Octubre Noviembre Diciembre)
module Format
MONTHS = {
'Enero' => 1, 'Febrero' => 2, 'Marzo' => 3, 'Abril' => 4,
'Mayo' => 5, 'Junio' => 6, 'Julio' => 7, 'Agosto' => 8,
'Septiembre'=> 9, 'Octubre' =>10, 'Noviembre' =>11, 'Diciembre'=>12
}
end
end
But nothing changed after I fired up the server again.
I hope you can help me, thanks in advance.
From the documentation:
:use_month_names - Set to an array with 12 month names if you want to customize month names. Note: You can also use Rails’ i18n functionality for this.
So you can either do this:
<%= f.date_select :date, {:use_month_names => ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre','Diciembre']} %>
Or, for bones internationalisation points, replace the strings with t() method calls and use Rails's I18n localisation files:
<%= f.date_select :date, {:use_month_names => [t(:jan), t(:feb), t(:mar), t(:apr), t(:may), t(:jun), t(:jul), t(:aug), t(:sep), t(:oct), t(:nov), t(:dec)]} %>
In config/locales/es.yml
es:
jan: "Enero"
feb: "Febrero"
...
And then in config/application.rb set:
config.i18n.default_locale = :es
Bingo! :-)
In Rails 4:
(polish case:)
= f.datetime_select :start_time, prompt: {day: 'Dzień', month: 'Miesiąc', year: 'Rok'}
pl:
date:
order: ["year", "month", "day"]
month_names: ["Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień"]
it's all
in rails 5 > easiest and best scalable way
in your view:
<%= f.date_select :start_time %>
in your config/locales/en.yml add this :
en:
date:
order: ["day", "year", "month"]
month_names: ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "August", "September", "Oktober", "November", "December"]
Just change the month names in between the strings in whatever you want.
For rails 4.2.5.1 or higher you can use t('locale_parameter_name') instead t(:locale_parameter_name)
E.g:
:use_month_names => [t('jan'), t('feb'), t('mar'), t('apr'), t('may'), t('jun'), t('jul'), t('aug'), t('sep'), t('oct'), t('nov'), t('dec')]