1<?php
2/**
3 * 2007-2016 PrestaShop
4 *
5 * thirty bees is an extension to the PrestaShop e-commerce software developed by PrestaShop SA
6 * Copyright (C) 2017-2018 thirty bees
7 *
8 * NOTICE OF LICENSE
9 *
10 * This source file is subject to the Open Software License (OSL 3.0)
11 * that is bundled with this package in the file LICENSE.txt.
12 * It is also available through the world-wide-web at this URL:
13 * http://opensource.org/licenses/osl-3.0.php
14 * If you did not receive a copy of the license and are unable to
15 * obtain it through the world-wide-web, please send an email
16 * to license@thirtybees.com so we can send you a copy immediately.
17 *
18 * DISCLAIMER
19 *
20 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
21 * versions in the future. If you wish to customize PrestaShop for your
22 * needs please refer to https://www.thirtybees.com for more information.
23 *
24 *  @author    thirty bees <contact@thirtybees.com>
25 *  @author    PrestaShop SA <contact@prestashop.com>
26 *  @copyright 2017-2018 thirty bees
27 *  @copyright 2007-2016 PrestaShop SA
28 *  @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
29 *  PrestaShop is an internationally registered trademark & property of PrestaShop SA
30 */
31
32/**
33 * Class SupplyOrderStateCore
34 *
35 * @since 1.0.0
36 */
37class SupplyOrderStateCore extends ObjectModel
38{
39    // @codingStandardsIgnoreStart
40    /**
41     * @var string Name of the state
42     */
43    public $name;
44
45    /**
46     * @var bool Tells if a delivery note can be issued (i.e. the order has been validated)
47     */
48    public $delivery_note;
49
50    /**
51     * @var bool Tells if the order is still editable by an employee (i.e. you can add products)
52     */
53    public $editable;
54
55    /**
56     * @var bool Tells if the the order has been delivered
57     */
58    public $receipt_state;
59
60    /**
61     * @var bool Tells if the the order is in a state corresponding to a product pending receipt
62     */
63    public $pending_receipt;
64
65    /**
66     * @var bool Tells if the the order is in an enclosed state (i.e. terminated, canceled)
67     */
68    public $enclosed;
69
70    /**
71     * @var string Color used to display the state in the specified color (Ex. #FFFF00)
72     */
73    public $color;
74    // @codingStandardsIgnoreEnd
75
76    /**
77     * @see ObjectModel::$definition
78     */
79    public static $definition = [
80        'table'     => 'supply_order_state',
81        'primary'   => 'id_supply_order_state',
82        'multilang' => true,
83        'fields'    => [
84            'delivery_note'   => ['type' => self::TYPE_BOOL,                   'validate' => 'isBool'                                          ],
85            'editable'        => ['type' => self::TYPE_BOOL,                   'validate' => 'isBool'                                          ],
86            'receipt_state'   => ['type' => self::TYPE_BOOL,                   'validate' => 'isBool'                                          ],
87            'pending_receipt' => ['type' => self::TYPE_BOOL,                   'validate' => 'isBool'                                          ],
88            'enclosed'        => ['type' => self::TYPE_BOOL,                   'validate' => 'isBool'                                          ],
89            'color'           => ['type' => self::TYPE_STRING,                 'validate' => 'isColor'                                         ],
90            'name'            => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128],
91        ],
92    ];
93
94    /**
95     * @see ObjectModel::$webserviceParameters
96     */
97    protected $webserviceParameters = [
98        'objectsNodeName' => 'supply_order_states',
99        'objectNodeName' => 'supply_order_state',
100        'fields' => [],
101    ];
102
103    /**
104     * Gets the list of supply order statuses
105     *
106     * @param int $idStateReferrer Optional, used to know what state is available after this one
107     * @param int $idLang          Optional Id Language
108     *
109     * @return array States
110     *
111     * @throws PrestaShopDatabaseException
112     * @throws PrestaShopException
113     * @since   1.0.0
114     * @version 1.0.0 Initial version
115     */
116    public static function getSupplyOrderStates($idStateReferrer = null, $idLang = null)
117    {
118        if ($idLang == null) {
119            $idLang = Context::getContext()->language->id;
120        }
121
122        $query = new DbQuery();
123        $query->select('sl.name, s.id_supply_order_state');
124        $query->from('supply_order_state', 's');
125        $query->leftjoin('supply_order_state_lang', 'sl', 's.id_supply_order_state = sl.id_supply_order_state AND sl.id_lang='.(int) $idLang);
126
127        if (!is_null($idStateReferrer)) {
128            $isReceiptState = false;
129            $isEditable = false;
130            $isDeliveryNote = false;
131            $isPendingReceipt = false;
132
133            //check current state to see what state is available
134            $state = new SupplyOrderState((int) $idStateReferrer);
135            if (Validate::isLoadedObject($state)) {
136                $isReceiptState = $state->receipt_state;
137                $isEditable = $state->editable;
138                $isDeliveryNote = $state->delivery_note;
139                $isPendingReceipt = $state->pending_receipt;
140            }
141
142            $query->where('s.id_supply_order_state <> '.(int) $idStateReferrer);
143
144            //check first if the order is editable
145            if ($isEditable) {
146                $query->where('s.editable = 1 OR s.delivery_note = 1 OR s.enclosed = 1');
147            }
148            //check if the delivery note is available or if the state correspond to a pending receipt state
149            elseif ($isDeliveryNote || $isPendingReceipt) {
150                $query->where('(s.delivery_note = 0 AND s.editable = 0) OR s.enclosed = 1');
151            }
152            //check if the state correspond to a receipt state
153            elseif ($isReceiptState) {
154                $query->where('s.receipt_state = 1 AND s.id_supply_order_state > '.(int) $idStateReferrer);
155            }
156        }
157
158        return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
159    }
160
161    /**
162     * Gets the list of supply order statuses
163     *
164     * @param array $ids    Optional Do not include these ids in the result
165     * @param int   $idLang Optional
166     *
167     * @return array
168     *
169     * @throws PrestaShopDatabaseException
170     * @throws PrestaShopException
171     * @since   1.0.0
172     * @version 1.0.0 Initial version
173     */
174    public static function getStates($ids = null, $idLang = null)
175    {
176        if ($idLang == null) {
177            $idLang = Context::getContext()->language->id;
178        }
179
180        if ($ids && !is_array($ids)) {
181            $ids = [];
182        }
183
184        $query = new DbQuery();
185        $query->select('sl.name, s.id_supply_order_state');
186        $query->from('supply_order_state', 's');
187        $query->leftjoin('supply_order_state_lang', 'sl', 's.id_supply_order_state = sl.id_supply_order_state AND sl.id_lang='.(int) $idLang);
188        if ($ids) {
189            $query->where('s.id_supply_order_state NOT IN('.implode(',', array_map('intval', $ids)).')');
190        }
191
192        $query->orderBy('sl.name ASC');
193
194        return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
195    }
196}
197