Project

General

Profile

Project progress work #50 » Outbound.php

Yutthapong Sricha, 20 Oct 2023 08:13

 
1
<?
2

    
3
require_once ('DBTable.php');
4
require_once ('BasicEnum.php');
5
require_once ('StockCardRecord.php');
6
require_once ('Stock.php');
7
require_once ('Util.php');
8

    
9
final class Outbound extends DBTable
10
{
11

    
12
	public $__TABLE = 'Outbound';
13
	public static $__COLUMNS = array(
14
						'id' 				=> 0,
15
						'item_id'			=> 0,
16
						'item_dvsn_id'		=> 0,
17
						'customer_id'		=> '',
18
						'customer_dvsn_id'	=> 0,
19
						'package_type'		=> 0,
20
						'package_qty'		=> 0,
21
						'product_date' 		=> '',
22
						'qty'				=> 0,
23
						'location_id'		=> 0,
24
						'location_dvsn_id'	=> 0,
25
						'status'  			=> 0,
26
						'slip_divide'  		=> '',
27
						'create_dt' 		=> '',
28
						'create_user_id' 	=> 0,
29
						'update_dt' 		=> '',
30
						'update_user_id' 	=> 0,
31
						'act_type' 			=> '',
32
					);
33

    
34
	/**
35
	 * Insert record with generated ID
36
	 *
37
	 * @return generated ID
38
	 */
39
	public function add($ob_cols) {
40
		try {
41
			$id = parent::add($ob_cols);
42

    
43
			// Update or Add to Stock table
44
			$ob_cols['update_user_id'] = $ob_cols['create_user_id'];	
45
			$result_stockcard = (new StockCardRecord())->addStockCard_ob($ob_cols);
46

    
47
			$ob_cols['qty'] *= -1;
48
			$result = (new Stock())->updateQty($ob_cols);
49

    
50
		} catch (Exception $e) {
51
			$this::$logger->error("Caught exception: " . print_r(sqlsrv_errors(), true));
52
			return false;
53
		}
54

    
55
		return $id;
56
	}
57

    
58
	public function set_($ob_cols, $where, $diff, $old=null) {
59
		try {
60
			$rows_affected = parent::set($ob_cols, $where);
61

    
62
			// check Item and Location with original value
63
			if ($old == null) {
64
				// Update or Add to Stock table with old key(Item and Location)
65
				$ob_cols['qty'] = $diff * -1;
66
				$result = (new Stock())->updateQty($ob_cols);
67
			} else {
68
				$this::$logger->debug("Item or Location changed!");
69
				// Add the qty number with new key
70
				$ob_cols['qty'] *= -1;
71
				$result = (new Stock())->updateQty($ob_cols);
72

    
73
				// Deduct an old stock record with its old number
74
				$ob_cols['qty'] = $old['oqty'];
75
				$ob_cols['item_id'] = $old['oitem_id'];
76
				$ob_cols['item_dvsn_id'] = $old['oitem_dvsn_id'];
77
				$ob_cols['location_id'] = $old['olocation_id'];
78
				$ob_cols['location_dvsn_id'] = $old['olocation_dvsn_id'];
79
				$ob_cols['product_date'] = $old['oproduct_date'];
80
				$result = (new Stock())->updateQty($ob_cols);
81

    
82
			}
83

    
84
		} catch (Exception $e) {
85
			$this::$logger->error("Caught exception: " . print_r(sqlsrv_errors(), true));
86
			return false;
87
		}
88
		return $rows_affected;
89
	}
90

    
91
	public function remove($ob_cols) {
92
		try {
93
			$rows_affected = parent::remove("id = " . $ob_cols['id']);
94

    
95
			// Update or Add to Stock table
96
			//$ob_cols['qty'] = $ob_cols['qty'] * -1;
97
			$result = (new Stock())->updateQty($ob_cols);
98

    
99
		} catch (Exception $e) {
100
			$this::$logger->error("Caught exception: " . print_r(sqlsrv_errors(), true));
101
			return false;
102
		}
103
		return $rows_affected;
104
	}
105

    
106

    
107

    
108
		/**
109
	 * cancel
110
	 *
111
	 * @return true if update successfully, otherwise return false
112
	 */
113
	public function cancel($ob_cols, $where){
114
		try {
115
			
116
			$upd_inb = "UPDATE " . $this->__TABLE .
117
						" SET status = 3 ".
118
						" , update_user_id = '" . $ob_cols['update_user_id'] . "'" .
119
						" WHERE id='" . $ob_cols['id'] ."'";
120
			
121
			$rows_affected = (new DBFactory())->query($upd_inb);
122

    
123
			$upd_stock = "UPDATE Stock " .
124
							" SET qty = qty + " . $ob_cols['qty'] .
125
							" , update_user_id = '" . $ob_cols['update_user_id'] . "'" .
126
							" WHERE item_id='" . $ob_cols['item_id'] . "'" .
127
							"   AND item_dvsn_id=" . $ob_cols['item_dvsn_id'] .
128
							"   AND customer_id='" . $ob_cols['customer_id'] . "'".
129
							"   AND customer_dvsn_id='" . $ob_cols['customer_dvsn_id']. "'";
130

    
131
			$rows_affected = (new DBFactory())->query($upd_stock);
132

    
133
			$ob_cols['package_qty'] *= -1;
134
			$ob_cols['qty'] *= -1;
135

    
136
			$create_dt =  $ob_cols['create_dt']->format('Y-m-d H:i:s');
137
			unset($ob_cols['create_dt']);
138

    
139
			$ob_cols['create_dt'] = $create_dt;
140
			$result_stockcard = (new StockCardRecord())->addStockCard_ob($ob_cols)?1:0;
141

    
142
			return $result_stockcard;
143
		}catch (Exception $e) {
144
			$this::$logger->error("Caught exception: " . print_r(sqlsrv_errors(), true));
145
			return false;
146
		}
147
		
148
	}
149
}
(5-5/6)