getEvent()->getBlock();
if($block instanceof Mage_Checkout_Block_Onepage_Shipping_Method_Available || $block instanceof AW_Onestepcheckout_Block_Onestep_Form_Shippingmethod || $block instanceof Magecheckout_SecuredCheckout_Block_Checkout_Shipping_Available)
{
$transport = $observer->getEvent()->getTransport();
$html = $transport->getHtml();
if(strpos($html, 'postdk_postdk') !== false) {
$html .= $block->getLayout()->createBlock('pacsoft/droppoints')->setTemplate('pacsoft/droppoints.phtml')->toHtml();
}
$transport->setHtml($html);
}
}
public function addMassAction($observer)
{
$block = $observer->getEvent()->getBlock();
if($block instanceof Mage_Adminhtml_Block_Widget_Grid_Massaction && strpos($block->getRequest()->getControllerName(),'sales_order') !== false)
{
$block->addItem('postdk_start', array(
'label' => '-- -- -- '.Mage::helper('pacsoft')->__('Pacsoft Online').' -- -- --',
'url' => '',
)
);
$block->addItem('postdk_auto', array(
'label' => Mage::helper('pacsoft')->__('Lav label ud fra leveringsform'),
'url' => Mage::helper('adminhtml')->getUrl('adminhtml/adminhtml_pacsoft_pacsoft/createLabel', array('_current'=>true)),
)
);
$block->addItem('postdk_auto_and_return', array(
'label' => Mage::helper('pacsoft')->__('Lav label ud fra leveringsform + Retur Label'),
'url' => Mage::helper('adminhtml')->getUrl('adminhtml/adminhtml_pacsoft_pacsoft/createLabelAndReturn', array('_current'=>true)),
)
);
if(Mage::getStoreConfig('pacsoft/settings/activate_standard_labels')) {
$block->addItem('postdk_private', array(
'label' => Mage::helper('pacsoft')->__('Lav standard privatlabel'),
'url' => Mage::helper('adminhtml')->getUrl('adminhtml/adminhtml_pacsoft_pacsoft/createPrivateLabel', array('_current'=>true)),
)
);
$block->addItem('postdk_business', array(
'label' => Mage::helper('pacsoft')->__('Lav standard erhvervslabel'),
'url' => Mage::helper('adminhtml')->getUrl('adminhtml/adminhtml_pacsoft_pacsoft/createBusinessLabel', array('_current'=>true)),
)
);
}
$block->addItem('postdk_return', array(
'label' => Mage::helper('pacsoft')->__('Lav returlabel'),
'url' => Mage::helper('adminhtml')->getUrl('adminhtml/adminhtml_pacsoft_pacsoft/createReturnLabel', array('_current'=>true)),
)
);
$block->addItem('postdk_end', array(
'label' => Mage::helper('pacsoft')->__('-- -- -- -- -- -- -- -- -- -- --'),
'url' => '',
)
);
}
}
public function saveData($event)
{
$prefix = "pacsoft-";
$request = Mage::app()->getRequest();
$quote = $event->getQuote();
$shippingMethod = $quote->getShippingAddress()->getData('shipping_method');
if(strpos($shippingMethod,'postdk') !== false)
{
$droppointsIds = Mage::helper('pacsoft/droppoints')->getDroppointsIds();
$flexIds = Mage::helper('pacsoft/droppoints')->getFlexIds();
$shippingMethod = explode("_",$shippingMethod);
if(in_array($shippingMethod[count($shippingMethod)-1],$droppointsIds))
{
$droppoint = $request->getPost($prefix.'droppoint-id');
if($droppoint == '') { return false; }
$droppoint = Mage::helper('pacsoft/droppoints')->getDroppoint($droppoint);
if(Mage::getStoreConfig('pacsoft/design/new_data_save_version'))
{
$firstname = trim($request->getPost($prefix."afhenter-firstname"));
$lastname = trim($request->getPost($prefix."afhenter-lastname"));
$company = trim($request->getPost($prefix."droppoint-name"));
$city = $request->getPost($prefix."droppoint-city");
$postcode = $request->getPost($prefix."droppoint-postalcode");
$street = $request->getPost($prefix."droppoint-streetname") . " " . $request->getPost($prefix."droppoint-streetnumber");
$telephone = $request->getPost($prefix."telephone");
$fax = 'Afhentningsid: ' . $droppoint;
$quote_shipping_address = $quote->getShippingAddress()
->setFirstname($firstname)
->setLastname($lastname)
->setCompany($company)
->setStreet($street)
->setPostcode($postcode)
->setCity($city)
->setTelephone($telephone)
->setFax($fax)
->setDroppoint(1); // droppoint true
try{
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_address');
$query = "UPDATE $table SET firstname=:firstname, lastname=:lastname, company=:company, street=:street, postcode=:postcode, city=:city, telephone=:telephone, fax=:fax, droppoint=1 WHERE quote_id=:quote_id AND address_type='shipping'";
$binds = array('firstname' => $firstname,
'lastname' => $lastname,
'company' => $company,
'street' => $street,
'postcode' => $postcode,
'city' => $city,
'telephone' => $telephone,
'fax' => $fax,
'quote_id' => $quote->getId(),
);
$write->query($query,$binds);
$quote->setShippingAddress($quote_shipping_address);
$quote->save();
}
catch(Exception $e) {
Mage::log($e->getMessage());
}
}
else
{
$name = explode(" ",trim($request->getPost($prefix."droppoint-name")));
$firstname = array_shift($name); // Giver første værdi
$lastname = (isset($name[0]) && $name[0]) ? implode(" ",$name) : "-";
$city = $request->getPost($prefix."droppoint-city");
$postcode = $request->getPost($prefix."droppoint-postalcode");
$street = $request->getPost($prefix."droppoint-streetname") . " " . $request->getPost($prefix."droppoint-streetnumber");
// Der explodes efterfølgende på ": " og skal derfor indgå i telefonnr-værdien!
$telephone = 'Afhentningsid: ' . $droppoint;
$quote_shipping_address = $quote->getShippingAddress()
->setFirstname($firstname)
->setLastname($lastname)
->setCompany('')
->setStreet($street)
->setPostcode($postcode)
->setCity($city)
->setTelephone($telephone)
->setFax('')
->setDroppoint(1); // droppoint true
try{
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_address');
$query = "UPDATE $table SET firstname=:firstname, lastname=:lastname, company='', street=:street, postcode=:postcode, city=:city, telephone=:telephone, fax='', droppoint=1 WHERE quote_id=:quote_id AND address_type='shipping'";
$binds = array('firstname' => $firstname,
'lastname' => $lastname,
'street' => $street,
'postcode' => $postcode,
'city' => $city,
'telephone' => $telephone,
'quote_id' => $quote->getId(),
);
$write->query($query,$binds);
$quote->setShippingAddress($quote_shipping_address);
$quote->save();
}
catch(Exception $e) {
Mage::log($e->getMessage());
}
}
}
elseif(in_array($shippingMethod[count($shippingMethod)-1],$flexIds))
{
try
{
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_address');
$query = "UPDATE $table SET droppoint=0 WHERE quote_id=:quote_id AND address_type='shipping'";
$binds = array('quote_id' => $quote->getId());
$write->query($query,$binds);
$quote_shipping_address = $quote->getShippingAddress()->setDroppoint(0);
$quote->setShippingAddress($quote_shipping_address);
$flexmessage = $request->getPost($prefix."flexmessage");
$quote->setPacsoftFlexmessage($flexmessage);
$quote->save();
}
catch(Exception $e) {
Mage::log($e->getMessage());
}
}
else
{
try {
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_address');
$query = "UPDATE $table SET droppoint=0 WHERE quote_id=:quote_id AND address_type='shipping'";
$binds = array('quote_id' => $quote->getId());
$write->query($query,$binds);
$quote_shipping_address = $quote->getShippingAddress()->setDroppoint(0);
$quote->setShippingAddress($quote_shipping_address);
$quote->save();
}
catch(Exception $e) {
Mage::log($e->getMessage());
}
}
}
}
public function handleSalesRulesActionForm($observer)
{
$form = $observer->getForm();
$actionsSelect = $form->getElement('simple_action');
if ($actionsSelect){
$actionsSelect->setValues(array_merge(
$actionsSelect->getValues(),
array(TRIC_Pacsoft_Model_Carrier_Postdk::FREE_SHIPPING_RATES => Mage::helper('pacsoft')->__('Fri fragt på specifikke Pacsoft fragtsatser'))
));
}
$values = array();
foreach(Mage::getModel('pacsoft/carrier_postdk')->getAllowedMethods() as $code => $label) {
$values[] = array('label' => $label,'value' => $code);
}
$fieldset = $form->getElement('action_fieldset');
$fieldset->addField(TRIC_Pacsoft_Model_Carrier_Postdk::FREE_SHIPPING_RATES_FIELD, 'multiselect', array(
'title' => Mage::helper('pacsoft')->__('Pacsoft Fragtsatser'),
'label' => Mage::helper('pacsoft')->__('Pacsoft Fragtsatser'),
'name' => TRIC_Pacsoft_Model_Carrier_Postdk::FREE_SHIPPING_RATES_FIELD,
'values' => $values,
'note' => Mage::helper('pacsoft')->__('Et beløb på 0, vil udløse fri fragt, mens et eventuelt beløb vil blive fratrukket fragtprisen. Der udløses ikke negative fragtsatser'),
));
return $this;
}
public function handlePacsoftSalesRulesActionData($observer)
{
$request = $observer->getEvent()->getRequest();
$values = $request->getParam(TRIC_Pacsoft_Model_Carrier_Postdk::FREE_SHIPPING_RATES_FIELD);
if($values && is_array($values)) {
$request->setPost(TRIC_Pacsoft_Model_Carrier_Postdk::FREE_SHIPPING_RATES_FIELD,implode(',',$values));
} else {
$request->setPost(TRIC_Pacsoft_Model_Carrier_Postdk::FREE_SHIPPING_RATES_FIELD,"");
}
}
public function specificFreeshipping($observer)
{
$quote = $observer->getEvent()->getQuote();
$quoteId = $quote->getId();
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('core_read');
$appliedRuleIds = $quote->getAppliedRuleIds();
if(!$appliedRuleIds && $quoteId){
$table = $resource->getTableName('sales/quote');
$query = "SELECT applied_rule_ids FROM $table WHERE entity_id = $quoteId";
$appliedRuleIds = $read->fetchOne($query);
}
if($appliedRuleIds)
{
$field = TRIC_Pacsoft_Model_Carrier_Postdk::FREE_SHIPPING_RATES_FIELD;
$simple_action = TRIC_Pacsoft_Model_Carrier_Postdk::FREE_SHIPPING_RATES;
$table = $resource->getTableName('salesrule');
$query = "SELECT $field,discount_amount FROM $table WHERE simple_action = '$simple_action' AND rule_id IN ($appliedRuleIds) ORDER BY sort_order ASC";
$results = $read->fetchAll($query);
if(count($results))
{
$discount_amounts = array();
$rate_codes = array();
foreach($results as $salesRule)
{
if(isset($salesRule[$field]) && $salesRule[$field])
{
$rate_codes = array_merge($rate_codes,explode(",",$salesRule[$field]));
foreach($rate_codes as $rate_code) {
if(isset($salesRule['discount_amount']) && $salesRule['discount_amount'] > 0 && !isset($discount_amounts[$rate_code])) {
$discount_amounts[$rate_code] = $salesRule['discount_amount'];
}
}
}
}
$rate_codes = array_unique($rate_codes);
$result = $observer->getEvent()->getResult();
foreach($result->getAllRates() as $method) {
if(in_array($method->getMethod(),$rate_codes)) {
if(isset($discount_amounts[$method->getMethod()])) {
$method->setPrice(max($method->getPrice()-$discount_amounts[$method->getMethod()],0));
} else {
$method->setPrice(0);
}
}
}
}
}
}
public function getSalesRulesActionJsScript()
{
$simple_action = TRIC_Pacsoft_Model_Carrier_Postdk::FREE_SHIPPING_RATES;
$field = TRIC_Pacsoft_Model_Carrier_Postdk::FREE_SHIPPING_RATES_FIELD;
$script = '';
return $script;
}
public function addSalesRulesActionFormJs($observer)
{
$block = $observer->getEvent()->getBlock();
if($block instanceof Mage_Adminhtml_Block_Promo_Quote_Edit)
{
$script = $this->getSalesRulesActionJsScript();
try{
if($gls = Mage::getModel('gls/observer')) {
if(method_exists($gls,'getSalesRulesActionJsScript')) {
$script .= $gls->getSalesRulesActionJsScript();
}
}
} catch(Exception $e) { }
try{
if($extrasalesrules = Mage::getModel('extrasalesrules/observer')) {
if(method_exists($extrasalesrules,'addSalesRulesActionFormJs')) {
$extrasalesrules->addSalesRulesActionFormJs($observer);
}
} else {
throw new Exception("Release Script");
}
}
catch(Exception $e)
{
$extra_scripts = array();
try{
if($pacsoft = Mage::getModel('pacsoft/carrier_postdk')) {
if(method_exists($pacsoft,'getExtraJsSalesRuleScript')) {
$extra_scripts[] = $pacsoft->getExtraJsSalesRuleScript();
}
}
} catch(Exception $e) {}
try{
if($gls = Mage::getModel('gls/carrier_gls')) {
if(method_exists($gls,'getExtraJsSalesRuleScript')) {
$extra_scripts[] = $gls->getExtraJsSalesRuleScript();
}
}
} catch(Exception $e) {}
$script .= '';
}
$transport = $observer->getEvent()->getTransport();
$html = $transport->getHtml();
$transport->setHtml($html.$script);
}
}
}