Subversion Repositories SmartDukaan

Rev

Rev 6963 | Rev 7090 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4500 mandeep.dh 1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
3
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4
 
5
<mapper namespace="in.shop2020.warehouse.persistence.ScanMapper">
6
  <resultMap type="scan" id="scanResult">
7
    <id property="id" column="id"/>
8
  </resultMap>
9
 
10
  <insert id="insert" parameterType="scan" useGeneratedKeys="true" keyProperty="id">
11
    INSERT INTO scanNew
12
      (inventoryItemId, warehouseId, type, scannedAt, quantity, orderId)
13
    VALUES
14
      (#{inventoryItemId}, #{warehouseId}, #{type}, NOW(), #{quantity}, #{orderId})
15
  </insert>
16
 
17
  <select id="get" parameterType="map" resultType="scan">
18
    SELECT * 
19
    FROM scanNew
20
    WHERE inventoryItemId = #{inventoryItemId}
21
    <if test="scanType != null">
22
        AND type = #{scanType}
23
    </if>
24
    ORDER BY scannedAt
25
  </select>
5361 mandeep.dh 26
 
27
  <select id="getScansFromOrderId" parameterType="java.lang.Long" resultType="scan">
28
    SELECT * 
29
    FROM scanNew
30
    WHERE orderId = #{orderId}
31
    ORDER BY scannedAt
32
  </select>
5372 mandeep.dh 33
 
34
  <select id="getPurchaseScans" parameterType="map" resultType="detailedPurchaseScan">
5395 mandeep.dh 35
    SELECT p.purchaseOrder_id AS purchaseOrderId, 1000 * UNIX_TIMESTAMP(po.createdAt) AS poCreatedAt, s.name AS supplierName,
6494 amar.kumar 36
        GROUP_CONCAT(DISTINCT(p.invoiceNumber)) AS invoiceNumbers, GROUP_CONCAT(DISTINCT(inv.receivedFrom)) AS receivedBy, i.itemId,
6361 rajveer 37
        brand, modelName, modelNumber, color, unitPrice, SUM(initialQuantity) AS quantity, 1000 * UNIX_TIMESTAMP(receivedOn) as purchasedAt, p.id as purchaseId 
5372 mandeep.dh 38
    FROM purchase p
39
    JOIN purchaseorder po 
40
        ON (po.id = p.purchaseOrder_id)
41
    JOIN inventoryItem i
42
        ON (p.id = i.purchaseId)
43
    JOIN lineitem l
44
        ON (l.itemId = i.itemId AND l.purchaseOrder_id = po.id)
45
    JOIN supplier s
46
        ON (s.id = po.supplierId)
6494 amar.kumar 47
    JOIN invoice inv
48
    	ON (inv.invoiceNumber = p.invoiceNumber)
5372 mandeep.dh 49
    WHERE po.createdAt BETWEEN #{startDate} AND #{endDate} 
6364 rajveer 50
    AND lastScanType != 'DOA_REPLACED' 
6362 rajveer 51
    GROUP BY p.purchaseOrder_id, p.id, i.itemId
52
    ORDER BY p.purchaseOrder_id, p.id, i.itemId
5372 mandeep.dh 53
  </select>
5496 mandeep.dh 54
 
55
  <select id="fetchMismatchScansPerInvoiceNumber" parameterType="java.util.Date" resultType="invoiceScan" >
56
    SELECT i.invoiceNumber, i.numItems, sum(initialQuantity) as scannedQuantity, i.date, s.name as supplierName
57
    FROM invoice i
58
    JOIN supplier s
59
    LEFT JOIN purchase p
60
        ON (p.invoiceNumber = i.invoiceNumber)
7089 amar.kumar 61
    LEFT JOIN inventoryItem ii 
62
    	ON (ii.purchaseId = p.id)
63
    JOIN scanNew sn
64
    	ON (sn.inventoryItemId = ii.id)
5496 mandeep.dh 65
    WHERE i.date = #{date}
7089 amar.kumar 66
    AND	sn.scannedAt between #{date} and ADDDATE(#{date},1)
67
    AND sn.type = 'PURCHASE'
68
    AND s.id !=1
5496 mandeep.dh 69
    GROUP BY i.invoiceNumber, i.numItems
70
    HAVING SUM(initialQuantity) != i.numItems
71
  </select>
5372 mandeep.dh 72
 
5711 mandeep.dh 73
  <select id="getInventoryAge" resultType="inventoryAge">
74
    SELECT t.itemId, brand, modelName, modelNumber, color,
75
        SUM((age &lt; 1) * currentQuantity) AS freshCount, 
5768 mandeep.dh 76
        SUM((age &gt;= 1 and age &lt; 2) * currentQuantity) AS oneToTwoCount, 
77
        SUM((age &gt;= 2 and age &lt; 3) * currentQuantity) AS twoToThreeCount, 
78
        SUM((age &gt;= 3 and age &lt; 4) * currentQuantity) AS threeToFourCount, 
79
        SUM((age &gt;= 4) * currentQuantity) as fourPlusCount,
80
        SUM((age &gt;= 1) * currentQuantity) as onePlusCount,
81
        SUM((age &gt;= 1) * unitPrice) as onePlusCost,
82
        SUM(currentQuantity) as zeroPlusCount,
83
        SUM(l.unitPrice) as zeroPlusCost,
84
        l.productGroup as category
5711 mandeep.dh 85
    FROM  (SELECT ii.id, ii.itemId, ii.currentQuantity, ii.purchaseId,
86
                DATEDIFF(now(), MAX(s.scannedAt)) / 7 as age
87
           FROM inventoryItem ii 
88
           JOIN scanNew s 
89
                ON (s.inventoryItemId = ii.id) 
90
           WHERE ii.lastScanType IN ('PURCHASE', 'SALE_RET') 
91
           GROUP BY ii.id, ii.itemId, ii.currentQuantity) t 
92
    JOIN purchase p 
93
        ON (p.id = t.purchaseId) 
94
    JOIN lineitem l 
95
        ON (l.purchaseOrder_id = p.purchaseOrder_id AND l.itemId = t.itemId)
96
    GROUP BY t.itemId 
97
    HAVING SUM(currentQuantity) != 0
98
  </select>
6322 amar.kumar 99
 
7089 amar.kumar 100
  <!--select id="inventoryAgeOnePlus" resultType="detailedInventoryAge">
101
    SELECT t.itemId, brand, modelName, modelNumber, color, scannedAt, i.warehouseId
102
        SUM((age &gt;= 1) * currentQuantity) AS onePlusCount, 
103
        SUM((age &gt;= 1) * unitPrice) as onePlusCost,
104
        l.productGroup as category
105
    FROM  (SELECT ii.id, ii.itemId, ii.currentQuantity, ii.purchaseId, MIN(s.scannedAt)
106
                DATEDIFF(now(), MAX(s.scannedAt)) / 7 as age
107
           FROM inventoryItem ii 
108
           JOIN scanNew s 
109
                ON (s.inventoryItemId = ii.id) 
110
           WHERE ii.lastScanType IN ('PURCHASE', 'SALE_RET') 
111
           GROUP BY ii.id, ii.itemId, ii.currentQuantity) t 
112
    JOIN purchase p 
113
        ON (p.id = t.purchaseId) 
114
    JOIN lineitem l 
115
        ON (l.purchaseOrder_id = p.purchaseOrder_id AND l.itemId = t.itemId)
116
    GROUP BY t.itemId, i.warehouseId 
117
    HAVING SUM(currentQuantity) != 0
118
  </select-->
119
 
120
 
121
 
6322 amar.kumar 122
  <select id = "getScansForItem" parameterType = "map" resultType = "scan">
123
	  SELECT s.id, s.inventoryItemId, s.warehouseId, s.type, s.scannedAt, 
124
	  s.quantity, s.orderId from scanNew s JOIN inventoryItem i 
125
	  ON 
126
	  s.inventoryItemId = i.id 
127
	  AND
6414 amar.kumar 128
	  i.itemId = #{itemId} 
6322 amar.kumar 129
	  AND
130
	  s.scannedAt BETWEEN #{fromDate} AND #{toDate};
131
  </select>
132
 
6558 amar.kumar 133
  <insert id="genericScan" parameterType="scan" useGeneratedKeys="true" keyProperty="id">
6467 amar.kumar 134
    INSERT INTO scanNew
135
      (inventoryItemId, warehouseId, type, scannedAt, quantity)
136
    VALUES
137
      (#{inventoryItemId}, #{warehouseId}, #{type}, NOW(), #{quantity})
138
  </insert>
139
 
6558 amar.kumar 140
  <select id = "getCurrentSerializedInventoryByScans" resultType = "inventoryAvailability">
141
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
6630 amar.kumar 142
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
6558 amar.kumar 143
	from 
144
	(select i.itemId, count(*) as inx from scanNew s 
145
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
146
		JOIN catalog.item c ON i.itemId = c.id 
147
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'SERIALIZED' 
148
		group by i.itemId ) as x 
6630 amar.kumar 149
	LEFT JOIN 
6558 amar.kumar 150
	(select i.itemId, count(*) as outx from scanNew s 
151
		JOIN inventoryItem i on i.id = s.inventoryItemId 
152
		JOIN catalog.item c ON i.itemId = c.id 
153
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'SERIALIZED' 
154
		group by i.itemId) as y 
155
	ON x.itemId = y.itemId 
156
	JOIN catalog.item ci ON ci.id = x.itemId 
6630 amar.kumar 157
	where ci.type = 'SERIALIZED' having quantity>0  
6558 amar.kumar 158
  </select>
6762 amar.kumar 159
 
160
<select id = "getHistoricSerializedInventoryByScans" resultType = "inventoryAvailability" parameterType = "date">
161
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
162
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
163
	from 
164
	(select i.itemId, count(*) as inx from scanNew s 
165
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
166
		JOIN catalog.item c ON i.itemId = c.id 
167
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'SERIALIZED'
168
		and s.scannedAt &lt; #{date} 
169
		group by i.itemId ) as x 
170
	LEFT JOIN 
171
	(select i.itemId, count(*) as outx from scanNew s 
172
		JOIN inventoryItem i on i.id = s.inventoryItemId 
173
		JOIN catalog.item c ON i.itemId = c.id 
174
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'SERIALIZED'
175
		and s.scannedAt &lt; #{date} 
176
		group by i.itemId) as y 
177
	ON x.itemId = y.itemId 
178
	JOIN catalog.item ci ON ci.id = x.itemId 
179
	where ci.type = 'SERIALIZED' having quantity>0  
180
  </select>
181
 
6558 amar.kumar 182
 
6630 amar.kumar 183
  <select id = "getCurrentNonSerializedInventoryByScans" resultType = "inventoryAvailability">
184
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
185
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
186
	from 
187
	(select i.itemId, sum(quantity) as inx from scanNew s 
188
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
189
		JOIN catalog.item c ON i.itemId = c.id 
190
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'NON_SERIALIZED' 
191
		group by i.itemId ) as x 
192
	LEFT JOIN 
193
	(select i.itemId, sum(quantity) as outx from scanNew s 
194
		JOIN inventoryItem i on i.id = s.inventoryItemId 
195
		JOIN catalog.item c ON i.itemId = c.id 
196
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'NON_SERIALIZED' 
197
		group by i.itemId) as y 
198
	ON x.itemId = y.itemId 
199
	JOIN catalog.item ci ON ci.id = x.itemId 
200
	where ci.type = 'NON_SERIALIZED' having quantity>0  
201
  </select>
6762 amar.kumar 202
 
203
<select id = "getHistoricNonSerializedInventoryByScans" resultType = "inventoryAvailability" parameterType = "date">
204
	SELECT x.itemId, ci.brand, ci.model_name as modelName, 
205
	ci.model_number as modelNumber, ci.color as color, (x.inx-if(y.outx is null,0,y.outx)) as quantity
206
	from 
207
	(select i.itemId, sum(quantity) as inx from scanNew s 
208
		JOIN inventoryItem i ON (i.id = s.inventoryItemId) 
209
		JOIN catalog.item c ON i.itemId = c.id 
210
		where s.type in ('PURCHASE','SALE_RET') and c.type = 'NON_SERIALIZED' 
211
		and s.scannedAt &lt; #{date}
212
		group by i.itemId ) as x 
213
	LEFT JOIN 
214
	(select i.itemId, sum(quantity) as outx from scanNew s 
215
		JOIN inventoryItem i on i.id = s.inventoryItemId 
216
		JOIN catalog.item c ON i.itemId = c.id 
217
		where s.type in ('SALE','LOST_IN_WAREHOUSE','PURCHASE_RETURN') and c.type = 'NON_SERIALIZED'
218
		and s.scannedAt &lt; #{date} 
219
		group by i.itemId) as y 
220
	ON x.itemId = y.itemId 
221
	JOIN catalog.item ci ON ci.id = x.itemId 
222
	where ci.type = 'NON_SERIALIZED' having quantity>0  
223
  </select>
224
 
6880 amar.kumar 225
 <select id = "getMovementSerializedInventoryByScans" resultType = "inventoryMovement" parameterType = "map">
6762 amar.kumar 226
 	select itemId, brand, model_name as modelName, model_number as modelNumber, color, s.type, count(*) as quantity 
227
 	FROM inventoryItem i JOIN scanNew s ON i.id = s.inventoryItemId 
228
 	LEFT JOIN catalog.item c ON i.itemId = c.id 
6963 amar.kumar 229
 	where s.scannedAt between #{startDate} and #{endDate} and c.type = 'SERIALIZED'
230
 	and i.currentWarehouseId not in (1,2,6,9) 
6762 amar.kumar 231
 	group by itemId, s.type
232
 </select>
233
 
234
 <select id = "getMovementNonSerializedInventoryByScans" resultType = "inventoryMovement" parameterType = "map">
6880 amar.kumar 235
 	select itemId, brand, model_name as modelName, model_number as modelNumber, color, s.type, sum(quantity) as quantity 
6762 amar.kumar 236
 	FROM inventoryItem i JOIN scanNew s ON i.id = s.inventoryItemId 
237
 	LEFT JOIN catalog.item c ON i.itemId = c.id 
6963 amar.kumar 238
 	where s.scannedAt between #{startDate} and #{endDate} and c.type = 'NON_SERIALIZED'
239
 	and i.currentWarehouseId not in (1,2,6,9)  
6762 amar.kumar 240
 	group by itemId, s.type
6880 amar.kumar 241
 </select>
6630 amar.kumar 242
 
4500 mandeep.dh 243
</mapper>