Jump to content

Welcome, Guest!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Pedalman

AdminsOrderController: need some coding advice

Recommended Posts

I added today two collumns to oder overview I need and like to see in the overview:

1.) invoice no.

2.) date of order status update

These were more than easy to add since Thirtybees uses a bootstrap admin template and columns fit more or less when you add some. You will find the additions in the added code snippet.

However, I need a third collumn and this is the most important one for me

3.) If order status history contains payment option "pay via invoice" & not order status ID "payed" then show date of delivery (order status date of deliverstatus ID) and add e.g. 14 days to it and color it red. Else do not show or string 'OK' in green 🙂

You dig it. I use a 'private' payment option that allow customers to buy via inhouse invoice. After 14 days they should have paid the order. Thing is I very much often forget those orders and have problems to controll them. Ok, well, I have an extra order status for them saying "waiting for payment of invoice" but I also forget some time to set this one. So, this collumn could be handy to me since I would see at a glance which orders were checked out via "my inhouse invoice payment" and where I have not set the status "payment received".

PS:

Best, or an enhancement would be to to add function to invoice payment module to set a certain status automatically (could check via cron). Actually, my pretty old invoice payment options claims to have this functions but it never worked 😞 Advantage of this approach is that I can bundle order status with reminder email for customer.

/override/controllers/admin/AdminOrdersController.php

<?php

class AdminOrdersController extends AdminOrdersControllerCore
{ 

    // @codingStandardsIgnoreStart
    /** @var string $toolbar_title */
    public $toolbar_title;
    /** @var array $statuses_array */
    protected $statuses_array = [];
    // @codingStandardsIgnoreEnd

    /**
     * AdminOrdersControllerCore constructor.
     *
     * @since 1.0.0
     */
    public function __construct()
    {
        /*echo "__construct() OVERRIDE BORIS WORKING"; */
        $this->bootstrap = true;
        $this->table = 'order';
        $this->className = 'Order';
        $this->lang = false;
        $this->addRowAction('view');
        $this->explicitSelect = true;
        $this->allow_export = true;
        $this->deleted = false;
        $this->context = Context::getContext();

        $this->_select = '
		a.id_currency,
        a.id_order AS id_pdf,

        /* Boris add invoice and update date 2019.12*/
        a.invoice_number,
        a.date_upd, 
        /* //Boris add invoice and update date 2019.12*/

		CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) AS `customer`,
		osl.`name` AS `osname`,
		os.`color`,
		IF((SELECT so.id_order FROM `'._DB_PREFIX_.'orders` so WHERE so.id_customer = a.id_customer AND so.id_order < a.id_order LIMIT 1) > 0, 0, 1) as new,
		country_lang.name as cname,
		IF(a.valid, 1, 0) badge_success';

        $this->_join = '
		LEFT JOIN `'._DB_PREFIX_.'customer` c ON (c.`id_customer` = a.`id_customer`)
		LEFT JOIN `'._DB_PREFIX_.'address` address ON address.id_address = a.id_address_delivery
		LEFT JOIN `'._DB_PREFIX_.'country` country ON address.id_country = country.id_country
		LEFT JOIN `'._DB_PREFIX_.'country_lang` country_lang ON (country.`id_country` = country_lang.`id_country` AND country_lang.`id_lang` = '.(int) $this->context->language->id.')
		LEFT JOIN `'._DB_PREFIX_.'order_state` os ON (os.`id_order_state` = a.`current_state`)
		LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = '.(int) $this->context->language->id.')';
        $this->_orderBy = 'id_order';
        $this->_orderWay = 'DESC';
        $this->_use_found_rows = true;

        $statuses = OrderState::getOrderStates((int) $this->context->language->id);
        foreach ($statuses as $status) {
            $this->statuses_array[$status['id_order_state']] = $status['name'];
        }

        $this->fields_list = [
            'id_order'  => [
                'title' => $this->l('ID'),
                'align' => 'text-center',
                'class' => 'fixed-width-xs',
            ],
            'reference' => [
                'title' => $this->l('Reference'),
            ],
            'new'       => [
                'title'          => $this->l('New client'),
                'align'          => 'text-center',
                'type'           => 'bool',
                'tmpTableFilter' => true,
                'orderby'        => false,
                'callback'       => 'printNewCustomer',
            ],
            'customer'  => [
                'title'        => $this->l('Customer'),
                'havingFilter' => true,
            ],
        ];

        if (Configuration::get('PS_B2B_ENABLE')) {
            $this->fields_list = array_merge(
                $this->fields_list,
                [
                    'company' => [
                        'title'      => $this->l('Company'),
                        'filter_key' => 'c!company',
                    ],
                ]
            );
        }

        $this->fields_list = array_merge(
            $this->fields_list,
            [
                'total_paid_tax_incl' => [
                    'title'         => $this->l('Total'),
                    'align'         => 'text-right',
                    'type'          => 'price',
                    'currency'      => true,
                    'callback'      => 'setOrderCurrency',
                    'badge_success' => true,
                ],
                'payment'             => [
                    'title' => $this->l('Payment'),
                ],
                'osname'              => [
                    'title'       => $this->l('Status'),
                    'type'        => 'select',
                    'color'       => 'color',
                    'list'        => $this->statuses_array,
                    'filter_key'  => 'os!id_order_state',
                    'filter_type' => 'int',
                    'order_key'   => 'osname',
                ],
                'date_add'            => [
                    'title'      => $this->l('Date'),
                    'align'      => 'text-right',
                    'type'       => 'datetime',
                    'filter_key' => 'a!date_add',
                ],
                /*BORIS 2019.12
                'invoice_date' => [
                    'title' => $this->l('Invoice Date'),
                    'align' => 'text-right',
                    'type' => 'datetime',
                    'filter_key' => 'a!invoice_date'
                ],*/
                'invoice_number' => [
                    'title' => $this->l('Invoice No'),
                    'align' => 'text-right',
                    'type' => 'int',
                    'filter_key' => 'a!invoice_number'
                ],
                'date_upd' => [
                    'title' => $this->l('Update OS'),
                    'align' => 'text-right',
                    'type' => 'datetime',
                    'filter_key' => 'a!date_upd'
                ],              
 /* //BORIS 2019.12*/

                'id_pdf'              => [
                    'title'          => $this->l('PDF'),
                    'align'          => 'text-center',
                    'callback'       => 'printPDFIcons',
                    'orderby'        => false,
                    'search'         => false,
                    'remove_onclick' => true,
                ],
            ]
        );

        if (Country::isCurrentlyUsed('country', true)) {
            $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(
                '
			SELECT DISTINCT c.id_country, cl.`name`
			FROM `'._DB_PREFIX_.'orders` o
			'.Shop::addSqlAssociation('orders', 'o').'
			INNER JOIN `'._DB_PREFIX_.'address` a ON a.id_address = o.id_address_delivery
			INNER JOIN `'._DB_PREFIX_.'country` c ON a.id_country = c.id_country
			INNER JOIN `'._DB_PREFIX_.'country_lang` cl ON (c.`id_country` = cl.`id_country` AND cl.`id_lang` = '.(int) $this->context->language->id.')
			ORDER BY cl.name ASC'
            );

            $countryArray = [];
            foreach ($result as $row) {
                $countryArray[$row['id_country']] = $row['name'];
            }

            $part1 = array_slice($this->fields_list, 0, 3);
            $part2 = array_slice($this->fields_list, 3);
            $part1['cname'] = [
                'title'       => $this->l('Delivery'),
                'type'        => 'select',
                'list'        => $countryArray,
                'filter_key'  => 'country!id_country',
                'filter_type' => 'int',
                'order_key'   => 'cname',
            ];
            $this->fields_list = array_merge($part1, $part2);
        }

        $this->shopLinkType = 'shop';
        $this->shopShareDatas = Shop::SHARE_ORDER;

        if (Tools::isSubmit('id_order')) {
            // Save context (in order to apply cart rule)
            $order = new Order((int) Tools::getValue('id_order'));
            $this->context->cart = new Cart($order->id_cart);
            $this->context->customer = new Customer($order->id_customer);
        }

        $this->bulk_actions = [
            'updateOrderStatus' => ['text' => $this->l('Change Order Status'), 'icon' => 'icon-refresh'],
        ];
       
        parent::__construct();
    }

}

 

Share this post


Link to post
Share on other sites
10 minutes ago, Pedalman said:

'new' => [ 'title' => $this->l('New client'), 'align' => 'text-center', 'type' => 'bool', 'tmpTableFilter' => true, 'orderby' => false, 'callback' => 'printNewCustomer', ],

If it's not easy to fetch by join, you can use callback parameter and fill field value by it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...