Copying fields within the same Model - OctoberCMS - laravel

Morning all,
I am trying to create a button that will copy the organisation address and populate the organisation billing address fields. I have no idea where to start.
The fields in question are:
// I want to copy these values
$organisation->email = Input::get('email');
$organisation->line_1 = Input::get('line_1');
$organisation->line_2 = Input::get('line_2');
$organisation->line_3 = Input::get('line_3');
$organisation->city = Input::get('city');
$organisation->state = Input::get('state');
$organisation->postcode = Input::get('postcode');
$organisation->country = Input::get('country');
// To these fields
$organisation->billing_line_1 = Input::get('billing_line_1');
$organisation->billing_line_2 = Input::get('billing_line_2');
$organisation->billing_line_3 = Input::get('billing_line_3');
$organisation->billing_city = Input::get('billing_city');
$organisation->billing_state = Input::get('billing_state');
$organisation->billing_postcode = Input::get('billing_postcode');
$organisation->billing_country = Input::get('billing_country');
Here is the start of my function
// Copy Address Button
public function onCopyAddress()
{
$organisation = Organisation::find($this->param('id'));
// Copy address logic here
$organisation->save();
Flash::success($organisation->name." Address has been successfully copied.");
return Redirect::to('/organisations/'.$organisation->id);
}

This is the solution I came up with.
public function onCopyAddress()
{
$organisation = Organisation::find($this->param('id'));
$organisation->billing_line_1 = Input::get('line_1');
$organisation->billing_line_2 = Input::get('line_2');
$organisation->billing_line_3 = Input::get('line_3');
$organisation->billing_city = Input::get('city');
$organisation->billing_state = Input::get('state');
$organisation->billing_postcode = Input::get('postcode');
$organisation->billing_country = Input::get('country');
$organisation->save();
Flash::success($organisation->name." Address has been successfully copied.");
return Redirect::to('/organisations/'.$organisation->id);
}

Related

Save data using pivot table

I want to save data for all user that have certain group_id in new table.
For example, i want to save new data to all user who have group_id = 1, but based on my code it only save user_id = 2. It should save user_id = 1 too.
$group = Group::find($req->groupID);
foreach($group->creators as $creator)
$attend = new Attendance;
$attend->groupID = $req->groupID;
$attend->meetingID = $data->id;
$attend->userFeedback = NULL;
$attend->userAttendance = 'Not Attend';
$attend->userID = $creator->id;
$attend->save();
You should run your loop properly. Follow the below code
$group = Group::find($req->groupID);
foreach($group->creators as $creator)
{
$attend = new Attendance();
$attend->groupID = $req->groupID;
$attend->meetingID = $data->id;
$attend->userFeedback = NULL;
$attend->userAttendance = 'Not Attend';
$attend->userID = $creator->id;
$attend->save();
}

Save ID two columns Laravel

I want to save the ID of a new Sale(); in $sale->voucher_series, how can I achieve this?
$sale = new Sale();
$sale->customerid = $request->customerid;
$sale->userid = \Auth::user()->id;
$sale->proof_type = $request->proof_type;
$sale->voucher_series = $request->saleId;
$sale->date_time = $mytime->toDateString();
$sale->total = $request->total;
$sale->status = 'Registered';
$sale->save();
If your voucher_series column is nullable field then it will work.
$sale = new Sale ();
$sale->customerid = $request->customerid;
$sale->userid = \Auth :: user()->id;
$sale->proof_type = $request->proof_type;
$sale->date_time = $mytime->toDateString ();
$sale->total = $request->total;
$sale->status = 'Registered';
$sale->save();
$sale->voucher_series = $sale->id;

Passing multiple values using

I want to pass multiple values in single column of database.
I have two tables (Schedule & Days). In form I select days when I select multiple days then I should add in db multiple. But it add only one day
public function store(Request $request)
{
$schedule = new menu;
$days = new day;
$sensor = new sensor;
$schedule->scheduleName = $request->name;
$schedule->start_time = $request->start_time;
$schedule->end_time = $request->end_time;
$schedule->timestamps = false;
$schedule->s_daytime = $request->s_daytime;
$schedule->e_daytime = $request->e_daytime;
$days->days = $request->days;
$days->timestamps = false;
$sensor->timestamps = false;
$sensor->sensor = $request->sensor;
$schedule->save();
$days->schedule_id = $schedule->id;
$days->save();
$sensor->schedule_id = $schedule->id;
$sensor->save();
return view('store');
}
What you can do is you can pass multiple days in an array and store the array in json format on your DB. here is the snippets.
$days_list = $request->days_list;
//for edit previous data
if(!empty($days->days_list)){
$arr = json_decode($days->days_list);
if(in_array($request->days, $arr)){
$data['success'] = false;
$data['message'] = 'This Day is already on your schedule
list.';
return $data;
}
array_push($arr, $days_list);
$days->days_list = json_encode($arr);
}
// for adding new data
else{
$days->days_list = json_encode(array($request->days_list));
}
$days->save();
```

Use IConfService to query object by Attributes

How do I query objects by attribute (instead of 'Filter Keys') using the Genesys Platform SDK?
Endpoint endpoint = new Endpoint("DEV", "the host", 12020);
endpoint.ServicePrincipalName = "the host/the principle";
_confServerProtocol = new ConfServerProtocol(endpoint);
_confServerProtocol.ClientApplicationType = (int)CfgAppType.CFGSCE;
_confServerProtocol.ClientName = "default";
_confServerProtocol.UserName = "the userid";
_confServerProtocol.Open();
IConfService confService = ConfServiceFactory.CreateConfService(_confServerProtocol);
CfgPersonQuery query = new CfgPersonQuery();
// Need to filter based on an Attribute Value (specifically externalID)
var foo = confService.RetrieveMultipleObjects<CfgPerson>(query);
This worked for me:
CfgXPathBasedQuery query = new CfgXPathBasedQuery(confService, CfgObjectType.CFGPerson, "CfgPerson[#externalID='the value']");
Use below:
Uri uri = new Uri("tcp://Host:Port");
Endpoint endpoint = new Endpoint(Guid.NewGuid().ToString(), uri);
ConfServerProtocol confProtocol = new ConfServerProtocol(endpoint);
confProtocol.ClientApplicationType = (int)CfgAppType.CFGSCE;
confProtocol.ClientName = "default";
confProtocol.UserName = "xxxxxx";
confProtocol.UserPassword = "xxxxxx";
//Channel Open
confProtocol.Open();
IConfService confService = ConfServiceFactory.CreateConfService(confProtocol);
CfgPersonQuery query = new CfgPersonQuery();
query.UserName = "AgentID";
CfgPerson person = confService.RetrieveObjects<CfgPerson>(query);
string ExtID = person.ExternalId;
Note: In this way, Filtering is not possible through ExternalId.

Master details batch crud with entity framework only saving single detail record

I'm building a master details page with batch editing, that is multiple details records with single master record. but only one detail record is being saved into the data base. I tried to debug & found that detail loop is executing multiple time accurately but not the saving multiple data. Here is my Code for save method:
public ActionResult CMN_VAL_FORM(HRM_CMN_VLU_MST_ViewModel model)
{
//var ctx=new Entities1();
var CMN_VLU_MST_OBJ = new HRM_CMN_VLU_MST();
var CMN_VLU_DTL_OBJ = new HRM_CMN_VLU_DTL();
//using (TransactionScope transaction = new TransactionScope())
//{
using (var ctx = new Entities1())
{
var type_code = ctx.ExecuteStoreQuery<string>("select get_pk_code('hrm_cmn_vlu_mst','CMN_VLU_TYPE_CODE') from dual").SingleOrDefault(); //A scalar function to generate the code in the format yymmdd0001
var value_code = ctx.ExecuteStoreQuery<string>("select get_pk_code('hrm_cmn_vlu_dtl','CMN_VLU_CODE') from dual").SingleOrDefault();
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_CODE = type_code;
CMN_VLU_MST_OBJ.CMN_VLU_REM = model.CMN_VLU_REM;
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_FOR = model.CMN_VLU_TYPE_FOR;
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_SRTNM = model.CMN_VLU_TYPE_SRTNM;
CMN_VLU_MST_OBJ.ENTRY_DATE = DateTime.Now;
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_NAME = model.CMN_VLU_TYPE_NAME;
foreach (var item in model.HRM_CMN_VLU_DTL)
{
CMN_VLU_DTL_OBJ.CMN_VLU_LEVL = item.CMN_VAL_LEVL;
CMN_VLU_DTL_OBJ.CMN_VLU_REM = item.CMN_VLU_REM;
CMN_VLU_DTL_OBJ.CMN_VLU_SLNO = item.CMN_VLU_SLNO;
CMN_VLU_DTL_OBJ.CMN_VLU_TITL = item.CMN_VAL_TITL;
CMN_VLU_DTL_OBJ.CMN_VLU_CNTN = item.CMN_VAL_CNTN;
CMN_VLU_DTL_OBJ.CMN_VLU_CODE = value_code;
CMN_VLU_DTL_OBJ.CMN_VLU_TYPE_CODE = type_code;
CMN_VLU_DTL_OBJ.ENTRY_DATE = DateTime.Now;
CMN_VLU_DTL_OBJ.MAIL_ADDR_INT = item.MAIL_ADDR_INT;
CMN_VLU_DTL_OBJ.MAIL_ADDR_EXT = item.MAIL_ADDR_EXT;
CMN_VLU_DTL_OBJ.MAIL_AUTO_SEND_INT = item.MAIL_AUTO_SEND_INT;
CMN_VLU_DTL_OBJ.MAIL_AUTO_SEND_EXT = item.MAIL_AUTO_SEND_EXT;
CMN_VLU_DTL_OBJ.ACTIVE_STATUS = item.ACTIVE_STATUS;
CMN_VLU_MST_OBJ.HRM_CMN_VLU_DTL.Add(CMN_VLU_DTL_OBJ);
ctx.SaveChanges();
var temp_value_code = Int32.Parse(value_code);
temp_value_code++;
value_code = temp_value_code.ToString();
ctx.HRM_CMN_VLU_MST.AddObject(CMN_VLU_MST_OBJ);
}
ctx.SaveChanges();
// transaction.Complete();
//}
}
return View();
}
No Error message for the code, but not saving multiple detail records. What I'm doing wrong?
What I was doing wrong, I created the object just once & updated that same object every time while executing the foreach loop! I just moved the object declaration into the foreach loop & it works like a charm!

Resources